[go: up one dir, main page]

0% found this document useful (0 votes)
30 views8 pages

SIMEC Exercise 1 Intro Matlab

Introduction to matlab exercise code

Uploaded by

ecmec22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

SIMEC Exercise 1 Intro Matlab

Introduction to matlab exercise code

Uploaded by

ecmec22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EXERCISE 1: INTRODUCTION TO MATLAB

2015

%% Introduction Matlab
% a) Compute with the matrix and the vectors:

a = [2;0;5];
b = [4;2;1];
c = [0;1;0];
A = [2 5 2;4 34 8;4 5 2];
B = [2 4 0;3 2 0;6 2 0];

% a*b
prodatb = a'*b;
prodabdot = dot(a,b);
prodabarray = a.*b;

% A+B
sumAB = A + B;

% A*b
prodAb = A*b;

% AT*c
prodAtc = A'*b;

% detA
detA = det(A);

% detB
detB = det(B);

% InverseA, InverseB
inversaA = inv(A);
inversaB = inv(B);

%% b) Try some exercises of matrix computation of mathematics using Matlab

% Solve the system


disp('Solve the system')
A = [2 5 2;4 34 8;4 5 2];
B = [2 4 0;3 2 0;6 2 0];
D = [1 -5 4;2 -7 3;-2 1 7];
b = [-3;-2;1];
solDb = rref([D b]);

% Find the rank of a matrix


D;
rankofD = rank(D);
rref(D);
A;
rankofA = rank(A);
rref(A);
B;
rankofB = rank(B);
rref(B);

% Partitioned matrices
E11 = [2 -3 1;1 5 -2];
E12 = [0 -4;3 -1];
E21 = [0 -4 -2];
E22 = [7 -1];
F1 = [6 4;-2 1;-3 7];
F2 = [-1 3;5 2];
E = [E11 E12;E21 E22];
F = [F1;F2];
prodEF = E*F;

% Eigenvalues
G = [2 2 2;2 2 2;2 2 2];
eig(G);

%% c) Plots
%Plot the functions sin cos tan arcsin arccos arctan in two plot using
%subplot. Comment the axis and fuctions in the graphic.
clear,clc,clf;
figure(1);
%plot sin,cos
x = -2*pi:0.01:2*pi;
subplot(2,1,1);
plot(x,sin(x),'-r',x,cos(x),'-.b');
grid on;
title('Graph sin(x),cos(x) vs. \theta');
xlabel('angle \theta [rad]');
ylabel('sin(x), cos(x)');
legend('sin(x)','cos(x)','Location','NorthEastOutside');
text(-2,5,0.7 ,'cos(x)');
text(2.7, 0.7 ,'sin(x)');
axis([-2*pi 2*pi -1.2 1.2]);
%plot tan
subplot(2,1,2);
plot(x,tan(x));
grid on;
title('Graph tan(x)vs. \theta');
xlabel('angle \theta [rad]');
ylabel('tan(x)');
legend('tan(x)','Location','NorthEastOutside');
text(3 , 15 ,'tan(x)');
axis([-2*pi 2*pi -100 100]);
%plot arcsin,arccos
figure(2);
z = [-1:0.001:1];
subplot(2,1,1);
plot(z,asin(z),'--k',z,acos(z));
grid on;
title('Graph arcsen(x),arccos(x) vs. x/x');
ylabel('angle \theta [rad]');
xlabel('x/x');
legend('arcsin(x)','arccos(x)','Location','SouthEast');
axis([-1 1 -2 4]);
%plot arctan
subplot(2,1,2);
plot(x,atan(x));
grid on;
title('Graph arctan(x)vs. \theta');
xlabel('angle \theta [rad]');
ylabel('arctan(x)');
legend('arctan(x)','Location','SouthEast');
text(3 , 3 ,'arctan(x)');
axis([-2*pi 2*pi -2 2]);
Graph sin(x),cos(x) vs. 

1 sin(x)
cos(x) sin(x) cos(x)
sin(x), cos(x)

0.5

-0.5

-1
-6 -4 -2 0 2 4 6
angle  [rad]
Graph tan(x)vs. 
100
tan(x)
50
tan(x)
tan(x)

-50

-100
-6 -4 -2 0 2 4 6
angle  [rad]

Graph arcsen(x),arccos(x) vs. x/x


4
angle  [rad]

0
arcsin(x)
arccos(x)
-2
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x/x arctan(x)
Graph arctan(x)vs. 
2

1
arctan(x)

-1
arctan(x)
-2
-6 -4 -2 0 2 4 6
angle  [rad]
% e) Build a function that builds the plot below
%The procedure to use the function may look like this

clear;
clf;
x = linspace(1,10,100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(sqrt(x));
hold on;
h1 = marcPlot(x,y1,'-r',5,'*r');
h2 = marcPlot(x,y2,'-g',6,'dg');
h3 = marcPlot(x,y3,'-b',7,'sb');
legend([h1,h2,h3],'y1','y2','y3');
xlabel('x');
ylabel('y');
title('some functions');
hold off

% Define the function as:

function h = marcPlot(x,y,lineStyle,nMark,markerStyle)

% [h] = marcPlot(x,y,lineStyle,nMark,markerStyle) plots the function


% y with respect to x, with the specidfied line style 'lineStyle',
% number of marks (between 1 and 12) over the function's plot 'nMark',
% and the specified marker style 'markerStyle'.
% Please refer to the following table:
%
% COLOR SPECIFIERS MARKER TYPE LINE STYLES
% b blue . point - solid
% g green o circle : dotted
% r red x x-mark -. dashdot
% c cyan + plus -- dashed
% m magenta * star (none) no line
% y yellow s square
% k black d diamond
% w white v triangle (down)
% ^ triangle (up)
% < triangle (left)
% > triangle (right)
% p pentagram
% h hexagram

%Check the input parameters


if nargin < 5 %Check if there are few input arguments
error('not enough input arguments please insert 5 input arguments
"x,y,lineStyle,nMark,markerStyle"');
elseif nargin > 6 %Check if there are many input arguments
error('too many input arguments');
elseif nargout > 1 %Check if there are many output requested arguments
error('too many output arguments');
elseif floor(nMark)/nMark ~= 1 %Check if nMark is an integer
error('nMark must be an integer');
end
if nMark > 12 || nMark < 1 %Check if nMark is between 1 and 12
error('this function only accepts nMark values between 1 and 12');
end

n = length(x);
count = 1;
n2 = (n/nMark);
for i=1:n
if 0 == mod(i,fix(n2))
xx(count) = x(i);
yy(count) = y(i);
count = count + 1;
end
end
h = plot(x,y,lineStyle,xx,yy,markerStyle);
end

some functions
1
y1
0.8 y2
y3
0.6

0.4

0.2

0
y

-0.2

-0.4

-0.6

-0.8

-1
1 2 3 4 5 6 7 8 9 10
x
% e) Build a function that builds the plot below
%The procedure to use the function may look like this

clear;
clf;
x = linspace(1,10,100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(sqrt(x));
hold on;
h1 = marcPlot(x,y1,'-r',5,'*r');
h2 = marcPlot(x,y2,'-g',6,'dg');
h3 = marcPlot(x,y3,'-b',7,'sb');
legend([h1,h2,h3],'y1','y2','y3');
xlabel('x');
ylabel('y');
title('some functions');
hold off

% Define the function as:

function h = marcPlot(x,y,lineStyle,nMark,markerStyle)

% [h] = marcPlot(x,y,lineStyle,nMark,markerStyle) plots the function


% y with respect to x, with the specidfied line style 'lineStyle',
% number of marks (between 1 and 12) over the function's plot 'nMark',
% and the specified marker style 'markerStyle'.
% Please refer to the following table:
%
% COLOR SPECIFIERS MARKER TYPE LINE STYLES
% b blue . point - solid
% g green o circle : dotted
% r red x x-mark -. dashdot
% c cyan + plus -- dashed
% m magenta * star (none) no line
% y yellow s square
% k black d diamond
% w white v triangle (down)
% ^ triangle (up)
% < triangle (left)
% > triangle (right)
% p pentagram
% h hexagram

%Check the input parameters


if nargin < 5
error('not enough input arguments please insert 5 input arguments
"x,y,lineStyle,nMark,markerStyle"');
elseif nargin > 6
error('too many input arguments');
elseif nargout > 1
error('too many output arguments');
elseif floor(nMark)/nMark ~= 1
error('nMark must be an integer');
end

n=length(x); %Returns the length of vector x


n2=fix(n/(nMark)); % Fixed space between marks
xx=(min(x):n2:n2*nMark); %New vector of equally spaced integer elements
yy=y(xx); %Asigns only the specified elements to yy
h = plot(x,y,lineStyle,x(xx),yy,markerStyle); %Plots both y and yy
end
some functions
1
y1
0.8 y2
y3
0.6

0.4

0.2

0
y

-0.2

-0.4

-0.6

-0.8

-1
1 2 3 4 5 6 7 8 9 10
x

You might also like