0% found this document useful (0 votes)
24 views

Matlab

This document contains examples of using MATLAB to perform various calculations and operations on matrices and vectors, including: 1) Solving a system of equations Ax=b using matrix division and displaying the result. 2) Concatenating strings and displaying the result. 3) Plotting sinusoidal functions with different frequencies. 4) Calculating Fibonacci numbers in a loop and plotting the ratios. 5) Calculating a cumulative sum in a loop and plotting it against an analytical solution.

Uploaded by

Rahul Saxena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Matlab

This document contains examples of using MATLAB to perform various calculations and operations on matrices and vectors, including: 1) Solving a system of equations Ax=b using matrix division and displaying the result. 2) Concatenating strings and displaying the result. 3) Plotting sinusoidal functions with different frequencies. 4) Calculating Fibonacci numbers in a loop and plotting the ratios. 5) Calculating a cumulative sum in a loop and plotting it against an analytical solution.

Uploaded by

Rahul Saxena
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

EXERCISE 17.

2
>> A=[2+2i -1 0;-1 2-2i -1;0 -1 2;]

A=

2.0000 + 2.0000i -1.0000


-1.0000
0

2.0000 - 2.0000i -1.0000


-1.0000

>> b=[1+i;0;1-i;]

b=

1.0000 + 1.0000i
0
1.0000 - 1.0000i

>> z=A\b

z=

0.6757 - 0.0541i
0.4595 + 0.2432i
0.7297 - 0.3784i

EXAMPLE 18.1
>> t1='A'

2.0000

t1 =

>> t2='BCDE'

t2 =

BCDE

>> t3=[t1,t2]

t3 =

ABCDE

>> x=-1:0.05;1

ans =

EXAMPLE 19.1
>> x=1:0.05:1;
>> for n =1:8
subplot(4,2,n),plot(x,sin(n*pi*x))
end
>> x=1:0.05:1;

>> subplot(4,2,n),plot(x,sin(n*pi*x),'g+')
>> end

>> x=1:0.05:1;
>> for n =1:8
subplot(4,2,n),plot(x,sin(n*pi*x),'g+')
end

EXAMPLE 19.2
>> for i=3:20
F(i)=F(i-1)+F(i-2);
end
>> plot(1:19,F(1:19)./F(2:20),'0')
Error using plot
Error in color/linetype argument

>> for i=3:20


F(i)=F(i-1)+F(i-2);
end
>> plot(1:19,F(1:19)./F(2:20),'g')

1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0

EXERCISE 19.3

>> S=zeros(100,1);
>> S(20)=sum(1./(1:20).^2);
>> for n= 21:200
S(n)=S(n-1)+ 1/n^2 ;
end
>> clf;plot(S,'.',[20,100],[1,1]*pi^2/6,'-')

10

12

14

16

18

20

Exercise 20.1
>> x=0:0.05:6;
>> y=sin(pi*x);
>> Y=(y>=0).*y;
>> plot(x,y,':',x,Y,'_')

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

Exercise 20.2
>> while s+(n+1)^2 <10
n=n+1;
s=s+n^2;
end
>> [n,s]

ans =

Example 20.2
>> n=1;d=1;
>> xold=pi/4;
>> while d>0.001 & n<20
n=n+1;
xnew=cos(xold);
xold=xnew;
end
>> [n,xnew,d]

ans =

20.0000 0.7391 1.0000

Example 21.1
> AREA=area(2,3,4)

AREA =

175.0065

>> AREA=area(10,15,20)

AREA =

175.0071

SOME BUILT IN FUNCTIONS


>> x=pi*(-1:3), round(x)

x=

-3.1416

0 3.1416 6.2832 9.4248

ans =

-3

>> fix(x)

ans =

-3

>> floor(x)

ans =

-4

>> ceil(x)

ans =

-3

7 10

>> sign(x)

ans =

-1

>> rem(x)
Error using rem
Not enough input arguments.

>> rem(x,3)

ans =

-0.1416

0 0.1416 0.2832 0.4248

>> a=[1 2 3;4 5 6;7 8 9;]

a=

>> s=sum(a)

s=

12 15 18

>> ss=sum(sum(a))

ss =

45

>> max(a)

ans =

>> max(abs(a))

ans =

Q 23.1 page 27
[x,y]=meshgrid(2:.2:4,1:.2:3);
z=(x-3).^2-(y-2).^2;
surf(x,y,z);

0.5

-0.5

-1
3
2.5

4
3.5

1.5

2.5
1

[x,y]=meshgrid(2:.2:4,1:.2:3);
z=(x-3).^2-(y-2).^2;
surfl(x,y,z);

0.5

-0.5

-1
3
2.5

4
3.5

1.5

2.5
1

Ex 23.2 page 27
[x,y]=meshgrid(-2:.2:2,-2:.2:2);
z=(-x)*y*exp((-2)*(x.^2+y.^2));
mesh(x,y,z);

0.1

0.05

-0.05

-0.1
2
1

2
1

-1

-1
-2

[x,y]=meshgrid(-2:.2:2,-2:.2:2);
z=(-x).*y.*exp((-2).*(x.^2+y.^2));
contour(x,y,z);

-2

2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-2

-1.5

-1

s=1;
n=1;
while s+(n+1)^2<10
n=n+1;
s=s+n^2;
end
disp('n = ');
disp(n);
disp('s =');
disp(s);

output
>> Untitled
n=
2
s=
5

Q 23.4 page 26
n=input('n');
d=floor(1+6*rand(n,2));

-0.5

0.5

1.5

disp(d);
disp(sum(d)/n);

output
>> dice
n

3
1

1.3333 3.0000

You might also like