New Matlab Programs
New Matlab Programs
New Matlab Programs
OUTPUT
[3 4 2; 2 1 5; 5 3 2]
[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.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
c = a .*+ b;
d = a ./ b;
e = a .^ b;
f = a';
f2 = b';
disp('A Matrix');
disp(a);
disp('B Matrx');
disp(b);
disp(c);
disp(d);
disp(e);
disp(f);
disp(f2);
OUTPUT
[3 4 5; 6 5 7; 1 2 3]
[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
18 28 40
24 15 42
9 16 21
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?
OUTPUT
Enter the Radius5
Radius of Circle = 5
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
Sum of 5 numbers = 15
Sum of 10 numbers = 55
ch = menu('Area','Circle','Triangle','Rectangle');
if ch == 1
r = input('Enter Radius');
A1 = pi*r^2;
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));
else
l = input('Enter Length');
br = input('Enter breadth');
A2=l*br;
end
OUTPUT
Enter Radius 6
Radius of Circle = 6
Enter a Value 3
Enter b Value 4
Enter c Value 6
Enter Length 10
Enter breadth 25
a = input('Enter a Value');
b = input('Enter b Value');
disp('1.Addtion');
disp('2.Subtraction');
disp('3.Multiplication');
disp('4.Division');
switch(ch)
case 1
c = a+b;
case 2
c = a-b;
case 3
c = a*b;
case 4
c = a/b;
otherwise
disp('Invalid Choice');
end
OUTPUT
Enter a Value 8
Enter b Value 4
1.Addtion
2.Subtraction
3.Multiplication
4.Division
>>
Enter a Value 56
Enter b Value 43
1.Addtion
2.Subtraction
3.Multiplication
4.Division
>>
Enter a Value 45
Enter b Value 65
1.Addtion
2.Subtraction
3.Multiplication
4.Division
>>
Enter a Value 67
Enter b Value 32
1.Addtion
2.Subtraction
3.Multiplication
4.Division
𝟑/𝟐 𝟐
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
>> x0=0;
>> [t,x]=ode23('simpode',tspan,x0)
>> plot(t,x)
>> xlabel('t')
>> ylabel('x')
Ode23 graph
II.
>> [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
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
>>t=0:10:90;
>>tI=0:90;
>>sI=spline(t,s,tI);
>>plot(tI,sI,’-k’);
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
>>x=A\B
>> [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=
D=
-10.3463 0 0
0 4.1693 0
1 0 10.1770