0% found this document useful (0 votes)
31 views4 pages

Cad QP

The document contains four MATLAB code examples: 1) Calculate the hypotenuse of a right triangle and area of the triangle given two sides. 2) Find the roots of two polynomials and their product polynomial. 3) Find the rank of two matrices and their bitwise multiplication. 4) Plot a cosine staircase signal.

Uploaded by

SUNIL KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
31 views4 pages

Cad QP

The document contains four MATLAB code examples: 1) Calculate the hypotenuse of a right triangle and area of the triangle given two sides. 2) Find the roots of two polynomials and their product polynomial. 3) Find the rank of two matrices and their bitwise multiplication. 4) Plot a cosine staircase signal.

Uploaded by

SUNIL KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 4

Compute the length of hypotenuse of a right triangle if the length of two other sides are

given abd find the area of triangle.


We can calculate the hypotenuse of a right triangle using this formula C2=A2+B2
A=input('enter the value of base ');
B=input('enter the value of perpendicular side ');
C=sqrt(A^2+B^2);
['length of hypotenuse']
disp(C)

two polynomials defines as P=[1 2 3 4] and Q=[1 0 3 4 5],write a program to find the roots of both
polynomials and find the roots of the polynomial which is the result of product of P and Q.
>> p=[1 2 3 4]

p=

1 2 3 4

>> roots(p)

ans =

-1.6506 + 0.0000i
-0.1747 + 1.5469i
-0.1747 - 1.5469i

>> q=[1 0 3 4 5]

q=

1 0 3 4 5

>> roots(q)

ans =

0.7445 + 1.8432i
0.7445 - 1.8432i
-0.7445 + 0.8432i
-0.7445 - 0.8432i

>> conv(p,q)

ans =

1 2 6 14 22 34 31 20
>> x=conv(p,q)
roots(x)

ans =

0.7445 + 1.8432i
0.7445 - 1.8432i
-0.1747 + 1.5469i
-0.1747 - 1.5469i
-1.6506 + 0.0000i
-0.7445 + 0.8432i
-0.7445 - 0.8432i

3) write matlab code to Find the rank of a matrix A=[1 2 3 ; 3 6 9 ; 5 9 7 ] and rank of matrix B=eye(3)
and bit wise multiplication of A and B.
>> A=[1 2 3; 3 6 9 ; 5 9 7]

A=

1 2 3
3 6 9
5 9 7

>> rank(A)

ans =

2
>> B=eye(3)

B=

1 0 0
0 1 0
0 0 1

>> rank(B)

ans =

3
>> C=A.*B

C=

1 0 0
0 6 0
0 0 7
4)Plot a cosine stair case plot having an amplitude of 2.5Volts and time period of 1second.
>> a = 0:0.1:50;
>> x= 2.5*cos(2*pi*a);
>> stairs(a,x)

20 Marks Questions
1. Write a for loop to compute the sum of the squares of all integers from 2 to 20:
Sum = 0; %initialize sum
for n = 2:20
Sum = Sum + n^2;
end
disp(Sum);

Sum=2869.
2. Write a matlab code to display all the prime numbers from 1 to 100
for i=2:100
for j=2:100
if(~mod(i,j))
break; % if factor found, not prime
end
end
if(j > (i/j))
fprintf('%d is prime\n', i);
end
end

3. Write a matlab code to solve the linear equations


x + 3y -2z = 5
3x + 5y + 6z = 7
2x + 4y + 3z = 8 and find the values of x,y and z.
Sol:

The equations can be written as

1 3 -2 x 5
3 5 6 * y =7
2 4 3 z 8

A * X =B
X=inv(A)*B
A=[1 3 -2;3 5 6;2 4 3]
B=[5;7; 6]
>> A=[1 3 -2;3 5 6;2 4 3]

A=

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

B=

5
7
6
>> C=inv(A)

C=

2.2500 4.2500 -7.0000


-0.7500 -1.7500 3.0000
-0.5000 -0.5000 1.0000

>> X=C*B

X=

-1.0000
2.0000
0
X=-1,y=2 z=0

You might also like