Exercise 1: Matlab part
1) Plot the function yx=2∙x-x2+sin(2∙x)∙cos(x) function problem1 (x) y=2.*x-x.^2+sin(2.*x).*cos(x) plot(x,y,'r') end 2) Print n! from n=2 to 20
function problem2 for k=2:20 n=factorial(k) end
3) Make a function that calculates RSS for a give vector.
function [rss]=problem3(a) rss=sqrt(a*a'); end
4) Make a function that check to see if a number is a prime.
function problem4(n) flag=0; for k=[2:1:(n/2)] a=rem(n,k); if a==0 flag=1; end end if flag==1 disp ('Number is not prime'); else disp('Number is prime'); end
5) Calculate a 3-degree nominal to fit the following column.
M=[-0.447,1.978,3.11,5.25,5.02,4.66,4.01,4.58,3.45,5.35,9.22]
Plot the curve of the nomial with the discrete points in the same graph.
m=[-0.447,1.978,3.11,5.25,5.02,4.66,4.01,4.58,3.45,5.35,9.22]; x1=1:numel(m); p=polyfit(x1,m,3); x2=0:0.01:(numel(m)+1); f=polyval(p,x2); plot(x1,m,'ko',x2,f,'r-') legend('n column','3-degNom','location','Best') grid on
6) Plot the following function as follow.
Rasx=20+x12+x22-10(cos2πx1+cos2πx2)
x1 and x2 are from 0 to 4.
x=0:0.05:4; y=0:0.05:4; [x,y]=meshgrid(x,y); z=20+x.*x+y.*y-10*(cos(2*pi*x)+cos(2*pi*y)) surf(x,y,z) grid on
7) Given the matrix N and P as follow, N=124736278123179324191347 P=23193829125849228923414913362248
Please calculate the N+P, N-P, N*P and P*N.
Please calculate the inverse matrix and orthogonal matrix of N.
N=[1,24,7,36;
2,7,8,12;
3,17,9,32
4,19,13,47]
P=[23,19,38,29;
12,58,49,22;
89,23,41,49;
13,36,22,48]
sum=N+P dif=N-P mulN=N*P mulP=P*N inverse=inv(N) orthogonal=orth(N) RESULTS:
N =
1 24 7 36 2 7 8 12 3 17 9 32 4 19 13 47
P =
23 19 38 29 12 58 49 22 89 23 41 49 13 36 22 48
sum =
24 43 45 65