PROBLEM 1:
Write a MATLAB Program which finds Fibonacci Series of n numbers.
Solution:
n = input('Enter the number of terms: ');
fib = zeros(1, n);
% First two Fibonacci numbers
if n >= 1
fib(1) = 0;
end
if n >= 2
fib(2) = 1;
end
% Generate the Fibonacci sequence
for i = 3:n
fib(i) = fib(i-1) + fib(i-2);
end
% Display the result
disp('Fibonacci series:');
disp(fib);
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);
if (AB==BC && BC==CD && CD==DA)
if AC==BD
disp('The QUADRILATERAL is a SQUARE.')
else
disp('The QUADRILATERAL is a RHOMBUS.')
end
elseif AB==CD && BC==DA
if AC==BD
disp('The QUADRILATERAL is a RECTANGLE.')
else
disp('The QUADRILATERAL is a PARALLELOGRAM')
end
elseif (slp_AB==slp_CD) || (slp_BC==slp_DA)
disp('This is a TRAPIZUAM.')
else
disp('This is a Special Type.')
end
%=============================================================================
% This Will Find The Area
%=============================================================================
M=[x1 y1 1;x2 y2 1;x3 y3 1];
N=[x1 y1 1;x3 y3 1;x4 y4 1];
Area=(1/2)*(abs(det(M))+abs(det(N)));
fprintf('\n The required area of the quadrilateral is %.3f sq. unit.\n',Area)
%======================================================
% This will show the Plot of the Quadrilateral.......
%======================================================
X=[x1 x2 x3 x4 x1]; %[4 4 8 6 4]
Y=[y1 y2 y3 y4 y1]; %[4 5 7 3 4]
plot(X,Y)
axis equal, axis on
grid on
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.
The required area of the quadrilateral is 7.000 sq. unit..
PROBLEM 4:
Write a MATLAB Program which finds the perimeter and area of triangle with the length of two
of triangle and the angle between two sides of the triangle.
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
the perimeter of the triangle is=14.650477/The area of the triangle is=5.250000
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]
% Calculate the lengths of the three sides
AB = norm(B - A);
BC = norm(C - B);
CA = norm(C - A);
% Set a tolerance for floating-point comparison
tol = 1e-5;
% Determine the triangle type
if abs(AB - BC) < tol && abs(BC - CA) < tol
triangleType = 'Equilateral';
elseif abs(AB - BC) < tol || abs(BC - CA) < tol || abs(CA - AB) < tol
triangleType = 'Isosceles';
else
triangleType = 'Scalene';
end
% Display the result
disp(['The triangle is ', triangleType, '.']);
% Plot the triangle
figure;
fill([A(1) B(1) C(1)], [A(2) B(2) C(2)], 'cyan', 'FaceAlpha', 0.3); % triangle
hold on;
plot([A(1) B(1) C(1) A(1)], [A(2) B(2) C(2) A(2)], 'b-o', 'LineWidth', 2); %
edges
% Label the vertices
text(A(1), A(2), ' A', 'FontSize', 12, 'Color', 'r', 'FontWeight', 'bold');
text(B(1), B(2), ' B', 'FontSize', 12, 'Color', 'r', 'FontWeight', 'bold');
text(C(1), C(2), ' C', 'FontSize', 12, 'Color', 'r', 'FontWeight', 'bold');
% Title and formatting
title(['Triangle Type: ', triangleType], 'FontSize', 14);
axis equal;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
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.
The roots are: -3.732051 -0.267949
PROBLEM 10:
Write a MATLAB Program to find the sum of the trigonometric series
sin(theta)+sin(2theta)*cos(theta)+... when theta is in degree.
Solution:
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
% Define the coefficiant of General form of 2nd Degree
a = input('The coefficient of x^2 = '); %a=2
b = input('The coefficient of y^2 = '); %b=2
h = input('The coefficient of xy = '); H=2*h; %h=4
g = input('The coefficient of x = '); G=2*g; %g=5
f = input('The coefficient of y = '); F=2*f; %f=6
c = input('The constant term = '); %c=7
% Define the 2nd degree equation
ge_2nd = @(x,y) a*x^2 + 2*h*x*y + b*y^2 + 2*g*x + 2*f*y + c;
fprintf('The given equation is :\t %d*x^2+%d*xy+%d*y^2+%d*x+%d*y+%d =
0\n',a,H,b,G,F,c);
% Check what does the given equation represent?
mat_delta=[a h g;h b f; g f c];delta=det(mat_delta);
if(delta==0)
if (a*b-h^2)==0
disp('It represents a pair of parallel straight lines.');
elseif ((a*b-h^2)<0)
disp('It represents a pair of intersecting straight lines');
else
disp('It represents a pair of straight lines.');
end
elseif (delta~=0)
if (a*b-h^2<0)
if (a+b)==0
disp('It represents a rectangular hyperbola.');
else
disp('It represents a hyperbola.');
end
elseif (a*b-h^2)>0
if(a==b) && (h==0)
disp('It represents a circle');
else
disp('It represents an ellips or the set is empty.');
end
else
disp('It represents a parabola');
end
end
% Plot the implicit equation
fimplicit(ge_2nd);%[-5 5 -5 5],'-b','LineWidth',1)
xlabel('x')
ylabel('y')
title('General Equation of Second Degree')
grid on;axis equal
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
% Draw X and Y - axes
plot([-7 7],[0 0])
plot([0 0],[-5 5])
axis equal
%Draw Director Circle
r=max(a,b);
x=r*cos(theta);
y=r*sin(theta);
plot(x,y,'r')
%Draw Auxiliary Circle
r=sqrt(a^2+b^2);
x=r*cos(theta);
y=r*sin(theta);
plot(x,y,'b')
title('Auxiliary and Director Circles of an Ellipse')
legend('Ellipse','Director Circle','Auxillary Circle')
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: