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

Matlab

This document contains examples of using MATLAB to perform matrix operations and solve equations, plot functions, use loops and conditional statements, and apply built-in functions like max, min, sum, and round. It demonstrates basics of MATLAB programming including matrix manipulation, plotting, conditional logic, and numerical methods.

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)
25 views

Matlab

This document contains examples of using MATLAB to perform matrix operations and solve equations, plot functions, use loops and conditional statements, and apply built-in functions like max, min, sum, and round. It demonstrates basics of MATLAB programming including matrix manipulation, plotting, conditional logic, and numerical methods.

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/ 9

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 =

You might also like