1.
Write a MATLAB Program for find SUM, SUB, MUL, DIVISION
of given two Matrices
a = input('Enter the First Matrix \n');
b = input('Enter the Second Matrix \n');
c = a+b;
d = a-b;
e = a*b;
f = a/b;
disp('A Matrix');
disp(a);
disp('B Matrx');
disp(b);
disp('Matrix Addition: ');
disp(c);
disp('Matrix Subtraction:');
disp(d);
disp('Matrix Multilication: ')
disp(e);
disp('Matrix Division: ');
disp(f);
OUTPUT
Enter the First Matrix
[3 4 2; 2 1 5; 5 3 2]
Enter the Second Matrix
[7 6 5; 2 3 4; 5 2 1]
A Matrix
3 4 2
2 1 5
5 3 2
B Matrx
7 6 5
2 3 4
5 2 1
Matrix Addition:
10 10 7
4 4 9
10 5 3
Matrix Subtraction:
-4 -2 -3
0 -2 1
0 1 1
Matrix Multilication:
39 34 33
41 25 19
51 43 39
Matrix Division:
1.9444 -1.5556 -1.5000
-2.6111 3.8889 2.5000
1.3889-0.1111 0.5000
2. Write a MATLAB Program for find element by Element matrix operations of
MUL,DIVISION,EXPONENT and Transpose of of given two Matrices
a = input('Enter the First Matrix \n');
b = input('Enter the Second Matrix \n');
c = a .*+ b;
d = a ./ b;
e = a .^ b;
f = a';
f2 = b';
disp('A Matrix');
disp(a);
disp('B Matrx');
disp(b);
disp('Matrix Multiplication Element by Element: ');
disp(c);
disp('Matrix Division Element by Element:');
disp(d);
disp('Matrix Exponentiation Element by Element: ')
disp(e);
disp('Transpose of A Matrix: ');
disp(f);
disp(f2);
OUTPUT
Enter the First Matrix
[3 4 5; 6 5 7; 1 2 3]
Enter the Second Matrix
[6 7 8; 4 3 6; 9 8 7]
A Matrix
3 4 5
6 5 7
1 2 3
B Matrx
6 7 8
4 3 6
9 8 7
Matrix Multiplication Element by Element:
18 28 40
24 15 42
9 16 21
Matrix Division Element by Element:
0.5000 0.5714 0.6250
1.5000 1.6667 1.1667
0.1111 0.2500 0.4286
Matrix Exponentiation Element by Element:
729 16384 390625
1296 125 117649
1 256 2187
Transpose of A Matrix:
3 6 1
4 5 2
5 7 3
Transpose of B Matrix:
6 4 9
7 3 8
8 6 7
3. Write a MATLAB Program for finding the Area and Circumference of a Circle?
r = input('Enter the Radius');
A = pi * r^2;
C= 2*pi*r;
fprintf('Radius of Circle = %d\n',r);
fprintf('Area of Circle = %f\n',A);
fprintf('Circumference of Circle = %d\n',C);
OUTPUT
Enter the Radius5
Radius of Circle = 5
Area of Circle = 78.539816
Circumference of Circle = 3.141593e+001
4. Write a MATLAB Program for finding Sum and Sum of Squares up to given numbers?
i = 1;
sum = 0;
sumsq = 0;
n = input('Enter How many Numbers');
for i = 1 : n
sum = sum + i;
sumsq= sumsq + i*i;
fprintf('Number = %d\t Square Number = %d\n',i,i*i);
end
fprintf('Sum of %d numbers = %d\n',n,sum);
fprintf('Sum of squares of %d numbers = %d\n',n,sumsq);
OUTPUT
1
Enter How many Numbers5
Number = 1 Square Number = 1
Number = 2 Square Number = 4
Number = 3 Square Number = 9
Number = 4 Square Number = 16
Number = 5 Square Number = 25
Sum of 5 numbers = 15
Sum of squares of 5 numbers = 55
Enter How many Numbers10
Number = 1 Square Number = 1
Number = 2 Square Number = 4
Number = 3 Square Number = 9
Number = 4 Square Number = 16
Number = 5 Square Number = 25
Number = 6 Square Number = 36
Number = 7 Square Number = 49
Number = 8 Square Number = 64
Number = 9 Square Number = 81
Number = 10 Square Number = 100
Sum of 10 numbers = 55
Sum of squares of 10 numbers = 385
5. Write a MATLAB Program for finding Area of Circle ,Triangle and Rectangle using
MENU options?
ch = menu('Area','Circle','Triangle','Rectangle');
if ch == 1
r = input('Enter Radius');
A1 = pi*r^2;
fprintf('Radius of Circle = %d \n',r);
fprintf('Area of Circle = %f \n',A1);
elseif ch == 2
a = input('Enter a Value');
b = input('Enter b Value');
c = input('Enter c Value');
s = (a+b+c)/2;
A= sqrt(s*(s-a)*(s-b)*(s-c));
fprintf('Side of a Triangle = %f \n',s);
fprintf('Area of Triangle = %d \f',A);
else
l = input('Enter Length');
br = input('Enter breadth');
A2=l*br;
fprintf('Area of Rectangle = %d \n',A2);
end
OUTPUT
Enter Radius 6
Radius of Circle = 6
Area of Circle = 113.097336
Enter a Value 3
Enter b Value 4
Enter c Value 6
Side of a Triangle = 6.500000
Area of Triangle = 5.332682e+000
Enter Length 10
Enter breadth 25
Area of Rectangle = 250
6. Write a MATLAB Program for finding Arithmetic operations of Two given Numbers
using SWITCH?
a = input('Enter a Value');
b = input('Enter b Value');
disp('1.Addtion');
disp('2.Subtraction');
disp('3.Multiplication');
disp('4.Division');
ch = input('Enter Your Choice');
switch(ch)
case 1
c = a+b;
fprintf('Addition of Two Numbers = %d\n',c);
case 2
c = a-b;
fprintf('Subtraction of Two Numbers = %d\n',c);
case 3
c = a*b;
fprintf('Multiplication of Two Numbers = %d\n',c);
case 4
c = a/b;
fprintf('Division of Two Numbers = %d\n',c);
otherwise
disp('Invalid Choice');
end
OUTPUT
Enter a Value 8
Enter b Value 4
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter Your Choice 1
Addition of Two Numbers = 12
>>
Enter a Value 56
Enter b Value 43
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter Your Choice 4
Division of Two Numbers = 1.302326e+000
>>
Enter a Value 45
Enter b Value 65
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter Your Choice 3
Multiplication of Two Numbers = 2925
>>
Enter a Value 67
Enter b Value 32
1.Addtion
2.Subtraction
3.Multiplication
4.Division
Enter Your Choice2
Subtraction of Two Numbers = 35
7. (a)Compute the Integral
𝟑/𝟐 𝟐
I.∫𝟏/𝟐 𝒆−𝒙 𝒅𝒙
𝟏 𝟐
II. ∫−𝟏 ∫𝟎 𝟏 − 𝟔𝒙𝟐 𝒚 𝒅𝒙𝒅𝒚
function y = int1(x);
y=exp(-x.^2);
end
>> y=quad('int1',1/2,3/2)
y=
0.3949
>> F = inline('1-6*x.^2*y');
>> I=dblquad(F,0,2,-1,1)
I=
4.0000
(b).Solve the First order Linear Differential Equation
I. dx/dt = x+t
With the initial condition x0=0
II.Solve the dy/dx = (x3-2y)/x
With the initial condition 1<x<3 y0=4.2
function xdot = simpode(t,x);
xdot = x+t;
end
>> tspan=[0 2];
>> x0=0;
>> [t,x]=ode23('simpode',tspan,x0)
>> plot(t,x)
>> xlabel('t')
>> ylabel('x')
Ode23 graph
II.
>> f=@(x,y) (x^3-2*y)/x
>> [x,y]=ode45(f,[1:0.01:3],4.2)
>>plot(x,y)
Ode45 graph
8. Write a Matlab program for Linear, Quadratic and Polynomial curve
fits for the following data and also estimate sales in the year
2018.Compare these 3 fits.Which is better?
Year 2000 2002 2004 2006 2008 2010 2012
Sales(in 45 32 62 75 95 114 144
lackhs)
x=[2000:2:2012];
y=[45,32,62,75,95,114,144];
p=polyfit(x,y,1);
y_fit=polyval(p,x);
plot(x,y,'ro',x,y_fit);
title('Sales of KFC company');
Xlabel('years');
Ylabel('Sales (in Thousands)');
x=[2000:2:2012];
y=[45,32,62,75,95,114,144];
p=polyfit(x,y,2);
y_fit=polyval(p,x);
plot(x,y,'ro',x,y_fit);
title('Sales of KFC company');
Xlabel('years');
Ylabel('Sales (in Thousands)');
x=[2000:2:2012];
y=[45,32,62,75,95,114,144];
p=polyfit(x,y,3);
y_fit=polyval(p,x);
plot(x,y,'ro',x,y_fit);
title('Sales of KFC company');
Xlabel('years');
Ylabel('Sales (in Thousands)');
Output
The estimated sales in the year 2018
By using linear curve is 187
By using Quadratic curve is 253
By using 3rd degree polynomial is 176
Therefore Quadratic curve is best fit for the given data
9. Create Bar , Pie and Area Chart of the following Data
continent Asia Europe Africa N.America S.America
Population 3332 696 694 437 307
cont=char('Asia', 'Europe','Africa','N.America','S.America');
pop=[3332;696;694;437;307];
barh(pop)
for i=1:5,
gtext(cont(i,:))
end
xlabel('Population in millions')
title('world population(1992)','fontsize',18)
cont=char('Asia', 'Europe','Africa','N.America','S.America');
pop=[3332;696;694;437;307];
pie(pop)
for i=1:5,
gtext(cont(i,:))
end
title('world population(1992)','fontsize',18)
x=linspace(-3*pi,3*pi,100);
y=-sin(x)./x;
area(x,y)
xlabel('x'),ylabel('sin(x)./x')
hold on
x1=x(46:55);
y1=y(46:55);
area(x1,y1,'facecolor','y')
10. Write a MATLAB Program to generate an overlay plot with the Line Command for
y1=sin(t)
y2=t
(t-t3)
y3 = ---------
( 6+t5/120)
t=linspace(0,2*pi,100);
y1=sin(t);y2=t;
y3=t-(t.^3)/6+(t.^5)/120;
plot(t,y1)
line(t,y2,'linestyle','--')
line(t,y3,'marker','o', ...
'linestyle','none')
axis([0 5 -1 5])
xlabel('t')
ylabel(' Approximation of sin(t)')
title('Fun with sin(t)')
legend('sin(t)','Linear Approx','Fifth-order-Approx')
OUTPUT
11. Write a MATLAB Program using Spline and Pchip Function
Time (sec) 0 10 20 30 40 50 60 70 80 90
Speed 45 32 0 0 7 12 20 15 29 55
Can taken 1 ½ min from this end of the road to that end of the road
%Speed in 1.5 minutes
>>t=0:10:90;
>> s=[45 32 0 0 7 12 20 15 29 55];
>>plot(t,s,’bo’); hold on;
%interpolation using spline
>>tI=0:90;
>>sI=spline(t,s,tI);
>>plot(tI,sI,’-k’);
%interpolation using PCHIP
sI_pc=pchip(t,s,tI);
plot(tI,sI_pc,’--r’);
OUTPUT
12. Write a MATLAB Program to find the Eigen values and Eigen vectors by using Gaussian
Elimination method?
5x =3y-2z+10
8y+4z=3x+20
2x+4y-9z=9
>> A=[5 -3 2; -3 8 4; 2 4 -9];
>> B=[10 ;20;9];
>>x=A\B
>> C=A*x %check the solution%
Gaussian Elimination method
>>C1=[A B] %form the augmented matrix%
>>Cr=rref(C1) %row reduce the augmented matrix%
Eigen values and Eigen Vectors
>> [V ,D]=eig(A)
Output
3.4442
x =[3.1982]
1.1868
10
C =[20]
9
C1=
5 -3 2 10
-3 8 4 20
2 4 -9 9
Cr =
1 0 0 3.4442
0 1 0 3.1982
0 0 1 1.1868
V=
-0.1709 0.8729 0.4570
-0.2365 0.4139 -0.8791
0.9565 0.2583 -0.1357
D=
-10.3463 0 0
0 4.1693 0
1 0 10.1770