You are on page 1of 12

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'
t1 =
A
>> t2='BCDE'
t2 =
BCDE
>> t3=[t1,t2]
t3 =
ABCDE

2.0000

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),'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

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

10

12

14

16

18

20

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 =
2

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,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 =
7

>> max(abs(a))
ans =
7

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

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');

-1

-0.5

0.5

1.5

d=floor(1+6*rand(n,2));
disp(d);
disp(sum(d)/n);

output
>> dice
n

3
1

1.3333 3.0000

RANDOM FUNCTION:
y=
0.9058
>> Y=rand(2,3)
Y=
0.1270 0.6324 0.2785
0.9134 0.0975 0.5469

FIND FOR NUMBERS


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

>> k=find(a==9)
k=
9

RIPPLING FUNCTION
function rippling(a,b)
n=101;
x=linspace(-2,2,n);
y=x;
[X,Y]=meshgrid(x,y);
for t=0:0.01:10
at=a+.8*t;
bt=b+.4*t;
Yxy=Y+.25*sin(2*(exp(X/2)-1));
Z=sin((at+0.1*X+.2*Yxy).*X.^2+...
bt*(sin(2*Yxy)+.2*Yxy).*Yxy.^2);
surf(X,Y,Z);
view([-45,60])
xlabel('x'),ylabel('y'),zlabel('z')
title(t)
drawnow
end
10

0.5

-0.5

-1
2

2
1.5

1.5
1

1
0.5

0.5
0

0
-0.5

-0.5
-1

-1
-1.5

-1.5
-2

-2

You might also like