Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Experiment 1 : Introduction to MATLAB

Example1. Find y = exp(52 / 3pi)


Solution:
>>a=5^2;
>>b=3*pi;
>>y=exp(a/b)
Output:

y=

14.1906

Exercise 1. Find the value of y = ln(sinh(exp(543 / 6* pi)))

Solution:

>>a=54^3;

>>b=6*pi;

>>c=exp(a/b);

>>d=sinh(c);

>>y=log10(d)

Output:

y=

Inf

Exercise 2: Find the size, and length of following matrices


A=[1 2 3; 4 5 6;7 6 54; 65 23 45]
B=7:1:13.5

Solution:

A=[1 2 3; 4 5 6;7 6 54; 65 23 45];

B=7:1:13.5;

>> size(A)

>>length(A)

>> size(B)

>>length(B)
Output:

ans =

4 3

ans =

ans =

1 7

ans =

Exercise 3. A=[2 3; 4 5]; B=[3 4; 6 7];


Find A+B, A*B, A.*B,A/B,A\B, A.^2,A./B

solution:

>>A=[2 3; 4 5]; B=[3 4; 6 7];


>>A+B;, A*B;, A.*B;,A/B;,A\B;, A.^2;A./B;
>>disp(A+B)
>>disp(A*B)
>>disp(A.*B)
>>disp(A/B)
>>disp(A\B)
>>disp(A.^2)
>>disp(A./B)

output:
5 7
10 12

24 29
42 51

6 12
24 35

1.3333 -0.3333
0.6667 0.3333

1.5000 0.5000
0 1.0000

4 9
16 25
0.6667 0.7500
0.6667 0.7143
Exercise 4. Plot the following functions in the same window y1=sin x, y2=sin 2x, y3=sin 3x,
y4=sin 4x where x varies from 0 to pi

Solution:

>>x1=0:0.1:pi;

>>y1=sin(x);

>>x2=0:0.1:pi;
>>y2=sin(2.*x);

>>x3=0:0.1:pi;
>>y3=sin(3.*x);

>>x4=0:0.1:pi;
>>y4=sin(4.*x);

>>figure(1)

>>subplot(2,2,1)
>>plot(x1,y1)

>>subplot(2,2,2)
>>plot(x2,y2)

>>subplot(2,2,3)
>>plot(x3,y3)

>>subplot(2,2,4)
>>plot(x4,y4)

output:
Example 7. WHILE

x=-10;
while x<0
x=x+1;
end
What is the value of x after execution of the above loop?

Solution:

>>x=-10;

>>while x<0
>>x=x+1;

>>disp(x)
end

output:

-9

-8

-7

-6

-5

-4

-3

-2

-1

Example 8. FOR loop:


x=0;
for i=1:10
x=x+1;
end
What is the value of x after execution of the above loop?

Solution:
x=0;
for i=1:10
x=x+1;
disp(x)
end

output:

10

Exercise 6 . Define the matrices


A=[17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
B=[ 2 3 4 5 ; 6 7 8 9 ; 10 11 12 13 ; 14 15 16 17 ]
C=[ 1 2 3 ; 4 5 6 ; 7 8 9 ]
y=[ 4 3 2 1 ]'
Note the transpose ' on the y-vector which makes y a column vector.
a) Compute AB and BA. Is matrix multiplication commutative?

b) Compute AC. Why do you get an error message?

Solution:
a)

A=[17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];


B=[ 2 3 4 5 ; 6 7 8 9 ; 10 11 12 13 ; 14 15 16 17 ];
C=[ 1 2 3 ; 4 5 6 ; 7 8 9 ];
A*B
B*A
Output:
ans =

132 158 184 210


228 254 280 306
356 398 440 482
484 542 600 658
ans =

150 132 146 160


326 260 290 320
502 388 434 480
678 516 578 640
The matrix multiplication commutative

b)
A=[17 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16];
B=[ 2 3 4 5 ; 6 7 8 9 ; 10 11 12 13 ; 14 15 16 17 ];
C=[ 1 2 3 ; 4 5 6 ; 7 8 9 ];
A*C

This multiplication is not possible

Yes ,I get an error expression

Output:

c) Solve the following system of equations:


17x1+2x2+3x3+4x4 = 4
5x1+6x2+7x3+8x4 = 3
9x1+10x2+11x3+12x4 = 2
13x1+14x2+15x3+16x4 = 1
Solution:
>> A=[17 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]

A=

17 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

>> B=[4;3;2;1]

B=

4
3
2
1

>> x=A^-1*B

Output:
x =

0.0000
9.0000
-21.5000
14.5000

Exercise 7 . Solve the following circuit to find i1, i2, and i3


Solution:
Apply kVL

3I1-I2-2I3=1
I1-6I2+3I3=0
2I1+3I2-6I3=-6
A=[3 -1 -2;1 -6 3;2 3 -6]

A=

3 -1 -2
1 -6 3
2 3 -6

>> B=[1;0;-6]

B=

1
0
-6

>>I =A^-1*B

Output:

I =

3.0000
2.0000
3.0000

You might also like