matlab

10
EXERCISE 1 1. >> A=[1;2;3] A = 1 2 3 >> B=[150 200;185 350;200 400] B = 150 200 185 350 200 400 >> C=[0 0 0;1 1 1;2 2 2;3 3 3;4 4 4] C = 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 2. >> B=[1:7;9:-2:-3] B = 1 2 3 4 5 6 7 9 7 5 3 1 -1 -3 (a). >>B(1:2,1)

Upload: florian-ananias-byarugaba

Post on 07-Sep-2015

2 views

Category:

Documents


0 download

DESCRIPTION

Matlab

TRANSCRIPT

EXERCISE 11.>> A=[1;2;3]A = 1 2 3>> B=[150 200;185 350;200 400]B = 150 200 185 350 200 400>> C=[0 0 0;1 1 1;2 2 2;3 3 3;4 4 4]C = 0 0 0 1 1 1 2 2 2 3 3 3 4 4 42.>> B=[1:7;9:-2:-3]B = 1 2 3 4 5 6 7 9 7 5 3 1 -1 -3(a).>>B(1:2,1)ans =

1 9(b).>>B(2,4)ans = 3(c).>>B(1:2,3:4)ans = 3 4 5 33.>> A=[1 2 3;4 5 6]A = 1 2 3 4 5 6>> B=[1:3;1:3]B = 1 2 3 1 2 3>> C=[5 7;8 9;10 15]C = 5 7 8 9 10 15>> A+Bans = 2 4 6 5 7 9>> B-A

ans = 0 0 0 -3 -3 -3>> B*Cans = 51 70 51 70>> A.*Bans = 1 4 9 4 10 184.>> A=[2 4 0 -1 3]A = 2 4 0 -1 3>> B=[2;5;8;3;-5]B = 2 5 8 3 -5>> A*(B+1)ans = 14

EXERCISE 21.>> %matlab expression for factorial of 12s=1;n=12;fori=1:n s=s*i;enddisp(s) 4790016002.>> %matlab expression for summation of square numbers up to 100s=0;n=100;fori=1:n s=s+i^2;enddisp(s) 3383503.%matlab expression for gaussian function of the given constantsx=0:0.01:1;a=1;a1=2;a2=10;a3=100;fori=x y=exp(-a*x.^2); y1=exp(-a1*x.^2); y2=exp(-a2*x.^2); y3=exp(-a3*x.^2);endsubplot(2,2,1)plot(x,y)

subplot(2,2,2)plot(x,y1)

subplot(2,2,3)plot(x,y2)

subplot(2,2,4)plot(x,y3) %exercise 3 %differentiation n=100;dx=2*pi/(n-1);

OUTPUT

EXERCISE 3>> %question 1n=101;dx=2*pi/(n-1);for k=1:n;x(k)=(k-1)*dxf(k)=cos((k+1)*dx)endplot(x,f)

Columns 97 through 101

0.9921 0.9980 1.0000 0.9980 0.9921

n=100;dx=2*pi/(n-1);for k=1:n;x(k)=(k-1)*dx;f(k)=cos((k-1)*dx);endfor k=1:n-1df(k)=(f(k+1)-f(k))/dx;end

for k=2:n-1df1(k)=(f(k+1)-f(k-1))/2*dx;error=df1(k)-df(k);endplot(x,error)

2.% integrationn=101;dx=0.01;I=0;%numerical solutionfori=1:nx(i)=(k-1)*dx;f(i)=(exp(-(i-1)*dx)); I=I+f(i)*dx;end>>disp(I) 0.6390BY TRAPEZOIDAL SOLUTIONsegment=100;point=segment +1;a=0;b=1;h=(b-a)(point-1);x=a:h:b;y=exp(-x);l=(h/2)*(y(1)+sum(y(2:point-1)))+y(point);disp (l) 0.6855