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

14 65 57 34

92 40 50 81

17 55 35 95

dif =

-22 5 -31 7

-10 -51 -41 -10

-86 -6 -32 -17

-9 -17 -9 -1

mulN =

1402 2868 2293 2628

998 1060 1011 1180

1490 2402 2020 2438

2088 3169 2650 3427

mulP =

291 1882 1032 3635

363 1945 1275 3730

454 3925 1813 7095

343 1850 1201 3860

inverse =

-0.4741 -0.3403 1.2821 -0.4229

0.0291 0.0129 0.1715 -0.1424

0.0858 0.2789 -0.3468 0.0992

0.0049 -0.0534 -0.0825 0.0874

orthogonal =

-0.5489 0.7770 0.0379 0.3058

-0.1952 -0.2831 0.9031 0.2571

-0.4733 0.0005 0.1452 -0.8688

-0.6607 -0.5622 -0.4023 0.2924

8) Design a simple calculator with a GUI.

function varargout = mycalc(varargin)

% MYCALC M-file for mycalc.fig

% MYCALC, by itself, creates a new MYCALC or raises the existing % singleton*.

%

% H = MYCALC returns the handle to a new MYCALC or the handle to % the existing singleton*.

%

% MYCALC('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in MYCALC.M with the given input arguments. %

% MYCALC('Property','Value',...) creates a new MYCALC or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before mycalc_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to mycalc_OpeningFcn via varargin. %

% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the...