MATLAB Assignment
MATLAB Assignment
Solution:
Output:
>> fibonacci
Enter the number of terms: 8
Fibonacci series:
0 1 1 2 3 5 8 13
PROBLEM 2:
Write a MATLAB Program which finds the perpendicular distance to the given line, the equation of the
perpendicular line, the equation of parallel line.
Solution:
clc
a=input('Enter the coefficient of a='); % a=3
b=input('Enter the coefficient of b='); % b=4
c=input('Enter the constant term='); % c=5
x1=input('x1='); % x1=2
y1=input('y1='); % y1=3
D=abs((a*x1+b*y1+c)/sqrt(a.^2+b.^2));
fprintf('The perpendicular distance to the given line is: %1.3f\n',D)
k1=(-a*x1-b*y1);
fprintf('The equn of the parallel line is: %dx + %dy %+d = 0\n',a,b,k1);
A=(-a);
k2=(-b*x1-A*y1);
fprintf('The equn of the perpendicular line is: %dx + %dy %+d = 0\n', b,A,k2);
Output:
Enter the coefficient of a=5
Enter the coefficient of b=2
Enter the constant term=2
x1=2
y1=3
The perpendicular distance to the given line is: 3.343
The equn of the parallel line is: 5x + 2y -16 = 0
The equn of the perpendicular line is: 2x + -5y +11 = 0
PROBLEM 3:
Write a MATLAB Program which finds the area of the Qquadrilateral and also investigate
the speciality of the quadilateral.
Solution:
clc
disp('======================================================================')
disp('This program finds the area of the quadrilateral.')
disp('======================================================================')
disp('Please give the four vertices..................')
%=============================================================================
% Insert point A.....
%=============================================================================
x1=input('x1=');
y1=input('y1=');
%=============================================================================
% Insert point B
%=============================================================================
x2=input('x2=');
y2=input('y2=');
%=============================================================================
% Insert point C.....
%=============================================================================
x3=input('x3=');
y3=input('y3=');
%=============================================================================
% Insert point D......
%=============================================================================
x4=input('x4=');
y4=input('y4=');
%=============================================================================
% This section of the program investigate whether the quadrilateral
% special type or not.........
%=============================================================================
AB=sqrt((x2-x1)^2+(y2-y1)^2); % Length of AB
BC=sqrt((x3-x2)^2+(y3-y2)^2); % Length of BC
CD=sqrt((x4-x3)^2+(y4-y3)^2); % Length of CD
DA=sqrt((x1-x4)^2+(y1-y4)^2); % Length of DA
AC=sqrt((x3-x1)^2+(y3-y1)^2); % Length of AC
BD=sqrt((x4-x2)^2+(y4-y2)^2); % Length of BD
slp_AB=(y2-y1)/(x2-x1);
slp_BC=(y3-y2)/(x3-x2);
slp_CD=(y4-y3)/(x4-x3);
slp_DA=(y1-y4)/(x1-x4);
Output:
===============================================================================
This program finds the area of the quadrilateral.
===============================================================================
Please give the four vertices..................
x1=4
y1=4
x2=4
y2=5
x3=8
y3=7
x4=6
y4=3
This is a Special Type.
Solution:
clc
a=input('Enter the 1st side of the triangle a='); % a=7
b=input('Enter the 2nd side of the triangle b='); % b=3
theta=input('Enter the angle between two sides of the triangle in degree c=') %
c=30
c=sqrt(a.^2+b.^2-2*a*b*cos(theta*pi/180));
peri=a+b+c;
s=peri/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
fprintf('the perimeter of the triangle is=%3f/',peri);
fprintf('The area of the triangle is=%3f',area);
Output:
Enter the 1st side of the triangle a=7
Enter the 2nd side of the triangle b=3
Enter the angle between two side of the triangle in degree c=30
theta =
30
PROBLEM 5:
Write a MATLAB Program which can draw graph based on Abscissa, Ordinate and Slope.
Solution:
clc
x1=input('Abscissa='); % x1=2
y1=input('Ordinate='); % y1=3
m=input('the slope of the straight line m='); % m=4
x=-2:0.01:2;
y=y1+m*(x-x1);
plot(x,y,'*m')
axis equal, grid on
Output:
Abscissa=2
Ordinate=3
the slope of the straight line m=4
PROBLEM 6:
Limit,continuity,differentiation:
Write a MATLAB Program which can find if the function is continuous or not.Can show the
graph of the function.
Solution:
clc
syms x h
f1 = x^2+x-1;
l1=limit(f1,x,0);
%f2 = (sin(x+h)-sin(x))/h;
%l2=limit(f2,h,0)
rhl=limit(f1,x,0,'right');
lhl=limit(f1,x,0,'left');
%g=@(x) x.^2+x-1;
%g1=f1(0);
df =diff(f1,x);g2=@(x)2*x+1;
if(rhl==lhl)&&(rhl==l1)
disp('The given function is Continuous');
else
disp('The given function is not Continuous');
end
x=linspace(-1,1,100);
%x=-1:0.01:1
plot(x,g2(x),'-r')
hold on
plot(x,g2(x),'-b')
Output:
The given function is Continuous
PROBLEM 7:
Write a MATLAB Program which can determine the type of any triangle by the three coordinates.
Solution:
clc
% Input the coordinates of the three vertices
A = input('Enter the coordinates of point A [x y]: '); % [4 1]
B = input('Enter the coordinates of point B [x y]: '); % [2 5]
C = input('Enter the coordinates of point C [x y]: '); % [6 3]
Output:
Enter the coordinates of point A [x y]: [4 1]
Enter the coordinates of point B [x y]: [2 5]
Enter the coordinates of point C [x y]: [6 3]
The triangle is Isosceles.
hwm.fig
PROBLEM 8:
Write a MATLAB Program which can draw graph of any function
Solution:
clc
clear figure
x=input('x='); % x=5
y=x.^2+1;
plot(x,y,'*')
text(x,y,'c(x,y)')
axis equal
axis on
xlabel('X axis')
ylabel('Y axis')
Output:
x=5
PROBLEM 9:
Write a MATLAB Program to determine the nature of the roots also find the roots of the equation.
Solution:
clc
a=input('The coefficient of x^n= '); %a=1
b=input('The coefficient of x^n-1='); %b=4
c=input('The constant term= '); %c=1
discr=(b^2-4*a*c);
if (discr==0)
disp('The roots are equal')
elseif (discr>0)
disp('The roots are real and distinct.')
else
disp('The roots are complex')
end
p=[a b c];
q=roots(p);
fprintf('\n The roots are: %f\t %f', q(1),q(2));
figure
title('2D Line Plot')
x=linspace(-1,1,1000);
y=a*x.^2+b*x+c;
plot(x,y,'DisplayName','Polynomial')
legend
grid on
xlabel('x')
ylabel('y')
Output:
The coefficient of x^n= 1
The coefficient of x^n-1=4
The constant term= 1
The roots are real and distinct.
clc
close all
disp(['The sum of the trigonometric series:'...
' sin(theta)+sin(2theta)*cos(theta)+... when theta is in degree'])
n= input('Enter the number of terms: n= '); %n=5
th=input('Enter the value of the angle in degree\n th='); %th=45
sum=0;
for i=1:n
sum=sum+ sin(i*th*pi/180)*cos(((i-1)*th)*pi/180);
end
fprintf('The sum of the first %d is %f', n, sum)
Output:
The sum of the trigonometric series: sin(theta)+sin(2theta)*cos(theta)+... when
theta is in degree
Enter the number of terms: n= 3
Enter the value of the angle in degree
th=35
The sum of the first 3 is 1.673694>>
PROBLEM 11: Write a MATLAB program to plot a piecewise-defined function involving absolute values,
defined as:
f(x)=∣x−a∣+∣x−b∣+∣x−c∣
Use the specific function:
𝑥 2 + 5 ; −5 < 𝑥 < 0
{ 5; 0≤𝑥≤3
9−𝑥; 𝑥>3
Additionally, take a test value as input and display which expression of the function it falls under.
Then, plot all three parts of the piecewise function on the same graph.
Solution:
% f(x)=abs(x-a)+abs(x-b)+abs(x-c)
% f(x)= {x^2+5, -5<x<0; 5, 0<=X<=3; 9-X, X>3
clc
p=input('The test value = '); %p=15
if (p>-5 && p<=0)
disp('y= x^2+5');
elseif (p>=0 && p<=3)
disp('y=5');
else
disp('y= 9-x');
end
x=-5:0.01:-0.01;y1=x.^2+5;
plot(x,y1,'-g')
grid on
hold on
x=0:0.01:3;y2=5*x.^0;
plot (x,y2,'.-b')
hold on
x=3.01:0.01:5;y3=9-x;
plot(x,y3,'.-m')
legend('y=x^2+5','y=5','y=9-x')
hold off
Output:
The test value = 15
y= 9-x
PROBLEM 12: Write a MATLAB program to classify and plot a conic section
represented by the general second-degree equation:
𝑎𝑥 2 + 2ℎ𝑥𝑦 + 𝑏𝑦 2 + 2𝑔𝑥 + 2𝑓𝑦 + 𝑐 = 0
Solution:
clc
close all
Output:
The coefficient of x^2 = 2
The coefficient of y^2 = 2
The coefficient of xy = 4
The coefficient of x = 5
The coefficient of y = 6
The constant term = 7
The given equation is : 2*x^2+8*xy+2*y^2+10*x+12*y+7 = 0
It represents a hyperbola.
PROBLEM 13: Write a MATLAB Program to draw an Auxiliary circle and Director circle of the ellipse:
2
𝑥 2⁄ + 𝑦 ⁄ = 1
𝑎2 𝑏2
Solution:
clc
clear
close all
disp(['Draw a Auxiliary circle and Director circle of'...
'the ellipse x^2/a^2+y^2/b^2=1'])
a=input('enter the value of a\n a='); %3
b=input('enter the value of b\n b='); %5
theta=linspace(0,2*pi,200);
x=a*cos(theta);
y=b*sin(theta);
plot(x,y,'k')
xlabel('X axis')
ylabel('Y axis')
grid on
hold on
Output:
Draw an Auxiliary circle and Director circle of the ellipse x^2/a^2 + y^2/b^2 =
1
Enter the value of a
a = 3
Enter the value of b
b = 5
PROBLEM 14: Write a MATLAB program to plot the following polar curves using polarplot and subplot:
1. r=a*sin(2(theta))
2. r=a*(1-cos(theta))
3. r=a*cos(2(theta))
4. r=a-b*cos(theta)
Solution:
clc
clear
close all
disp('Draw the polar curves r=asin(theta), r=a(1-cos(theta),...')
a=input('Enter the value of a\n a='); %2
b=input('Enter the value of b\n b='); %3
theta=linspace(0,2*pi,2000);
r=a*sin(2*theta);
subplot(2,2,1); polarplot(theta,r,'g')
legend('r-asin(2*t)')
r=a*(1-cos(theta));
subplot(2,2,2); polarplot(theta,r,'r')
legend('r=a(1-cos(t))')
r=a*cos(2*theta);
subplot(2,2,3); polarplot(theta,r,'k')
legend('acos(2t);')
r=a-b*cos(theta);
subplot(2,2,4); polarplot(theta,r,'m')
legend('r=a-b*cos(t)')
Output:
Draw the polar curves r=asin(theta), r=a(1-cos(theta),...
Enter the value of a
a=2
Enter the value of b
b=3
PROBLEM 15: Plot the hyperbolas x^2+y^2=1/2/3 by using fimplicit.
The fimplicit function uses the default interval of [-5,5] for x and y.
Solution:
clc
clear
close all
syms x y
hyperbola = x^2 - y^2;
fimplicit(hyperbola == 1,'-^b')
hold on
fimplicit(hyperbola == 2, '--or')
fimplicit(hyperbola == 3, '-.*c')
legend('show','Location','best')
grid on
hold off
Output: