Jaypee Institute Of Information Technology
Applied Mathematical Computational Lab
Submitted To:
Dr. Vivek kumar dwivedi
Submitted By:
Sarthak Lakhera
23119030
A18
INDEX
S.No Experiment Date Remarks
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Sarthak Lakhera 23119030 A18
Applied Mathematical Computational Lab
EXPERIMENT-1
Q1. A=[9,10,20,21,24] , B=[1,2,3,4,5] find minimum.maximum,mean , std. Variation and sum. Also
dot, cross product of A & B and inverse of (A/B).
Ans1.
A=[9,10,20,21,24];
B=[1,2,3,4,5];
a=mean(A);
b=max(A);
c=min(A);
d=sum(A);
e=median(A);
f=std(A);
g=dot(A,B);
i=inv(A./B);
j=rand(1,128);
Output-
a =16.8000
b =24
c =9
d =84
e =20
f =6.8337
g =293
i=0.1877
Q2. Generate random signal b/w 0 to 1 and 0 to 10 for N=128 .
Ans 1.
A=[9,10,20,21,24]
B=[1,2,3,4,5]
j=rand(1,128)
h=randperm(10)
Q3. Find out roots of polynomial equation 5x^5 + 7x^4+2x^2-6x+10 using matlab .
Ans 3.
x=[5 7 0 2 -6 10];
y=roots([5 7 0 2 -6 10]);
SARTHAK LAKHERA 23119030 A18
Sarthak lakhera 23119030 A18
Output-
y= -1.8652 + 0.0000i
-0.4641 + 1.0832i
-0.4641 - 1.0832i
0.6967 + 0.5355i
0.6967 - 0.5355i
Q4. Determine the eigen values and eigen vectors using
of A1 & B1 using matlab
A1= [4 2 -3 B1=[1 2 3
-1 1 3 846
ss
2 5 7] 5 3 1]
Learning Outcome:
Perform basic mathematical operations (mean, std, dot/cross product) and matrix manipulations
in MATLAB.
Solve polynomial equations and find eigenvalues/eigenvectors of matrices.
Generate and analyze random data for simulations and signal processing.
Sarthak Lakhera 23119030 A18
Sarthak lakhera 23119030 A18
EXPERIMENT 2
Objective:
To determine the least square fit and curve fit for given data points and plot them using suitable
mathematical models and functions like polyfit, polyval, and lsqcurvefit in MATLAB or similar
platforms.
Theory:
1. Introduction
Curve Fitting:
Curve fitting is the process of finding a curve (or mathematical function) that best fits a series of data
points. This is widely used in engineering, physics, and data analysis to predict trends and analyze
behaviors.
Least Squares Method:
The least squares method minimizes the sum of the squares of the differences between the observed
values and the values predicted by the model. The aim is to reduce the overall error between the
data points and the curve.
2. Types of Curve Fitting Models
1. Linear Curve Fitting:
o The model is a straight line y=ax+by = ax + by=ax+b, where aaa (slope) and bbb
(intercept) are determined using the least squares method.
2. Polynomial Curve Fitting:
o For higher-order fits, the model is a polynomial of degree n.
3. Non-Linear Curve Fitting:
o The model involves non-linear functions (e.g., exponential, logarithmic, sinusoidal).
3. Least Squares Method Theory
Given a set of data points (xi,yi) for i=1,2,…,Ni = :
Let f(x)f(x)f(x) represent the curve to be fitted.
The residual is given as ei=yi−f(xi).
The objective is to minimize the sum of squared residuals.
4. Curve Fitting Using Functions
4.1. Polyfit
This MATLAB function determines the coefficients of a polynomial fit:
p = polyfit(x, y, n);
x, y: Data points.
n: Degree of the polynomial.
Returns p, a vector of coefficients of the polynomial.
Sarthak Lakhera 23119030 A18
Sarthak lakhera 23119030 A18
4.2. Polyval
Evaluate the fitted polynomial for new or given x-values
y_fit = polyval(p, x);
p: Polynomial coefficients from polyfit.
x: Data points or a range of values for evaluation.
Returns the y-values of the fitted curve.
4.3. Lsqcurvefit
This function is used for fitting a non-linear model:
[param, resnorm] = lsqcurvefit(fun, param0, x, y);
fun: Handle to the function defining the model.
param0: Initial guess for the parameters.
x, y: Data points.
Returns optimized parameters and the residual norm.
4.4. User-Defined Function (fun)
You can define a custom model function for lsqcurvefit. For example:
fun = @(param, x) param(1) * exp(param(2) * x);
Here, the model is y=a⋅e^bx with param = [a, b].
Learning Outcome:
Understand least square fitting and curve fitting using mathematical models.
Apply functions like polyfit, polyval, and lsqcurvefit for data analysis in MATLAB.
Visualize and interpret fitted curves and their accuracy for given data points.
Sarthak Lakhera 23119030 A18
Sarthak Lakhera 23119030 A18
Ques 1 Given the data:
X-axis values: [1, 3, 5, 7, 9, 11, 13, 15]
Y-axis values: [4, 9, 23, 37, 73, 103, 133, 179]
close all;
clear all;
Xaxis = [1,3,5,7,9,11,13,15];
Yaxis = [4,9,23,37,73,103,133,179];
coeff = polyfit(Xaxis,Yaxis,1);
plt = polyval(coeff,Xaxis);
subplot(3,3,1);
plot(Xaxis, Yaxis);
coeff1 = polyfit(Xaxis,Yaxis,2);
plt1 = polyval(coeff1,Xaxis);
subplot(3,3,2);
plot(Xaxis, Yaxis );
coeff2 = polyfit(Xaxis,Yaxis,3);
plt2 = polyval(coeff2,Xaxis);
subplot(3,3,3);
plot(Xaxis, Yaxis);
coeff3 = polyfit(Xaxis,Yaxis,4);
plt3 = polyval(coeff3,Xaxis);
subplot(3,3,4);
plot(Xaxis, Yaxis);
coeff4 = polyfit(Xaxis,Yaxis,5);
plt4 = polyval(coeff4,Xaxis);
subplot(3,3,5);
plot(Xaxis, Yaxis);
ques 2
Xdata = [0.9, 0.15, 13.8, 19.8, 24.1, 28.2, 35.2, 60.3, 74.6, 81.3];
Ydata = [455.2, 428.2, 124.1, 67.3, 43.2, 28.3, 13.1, -0.4, -1.3, -1.5];
xfun = @(x, Xdata) x(1) .* exp(x(2) * Xdata);
x0 = [100, -1];
x = lsqcurvefit(xfun, x0, Xdata, Ydata);
Yfit = xfun(x, Xdata);
figure;
plot(Xdata, Ydata, 'o');
hold on;
plot(Xdata, Yfit, '-');
grid on;
Sarthak Lakhera 23119030 A18
Sarthak Lakhera 23119030 A18
xlabel('Xdata');
ylabel('Ydata');
title('Curve Fitting using Exponential Model');
ques 3 m=5,10,20,50,100 and g = 15.5,33,53.39,140.24,301
find out linear fit for the given data and plot it
m = [5, 10, 20, 50, 100];
g = [15.5, 33, 53.39, 140.24, 301];
coefficients = polyfit(m, g, 1);
slope = coefficients(1);
intercept = coefficients(2);
m_fit = linspace(min(m), max(m), 100);
g_fit = polyval(coefficients, m_fit);
plot(m, g, 'o');
hold on;
plot(m_fit, g_fit, '-');
grid on;
xlabel('m');
ylabel('g');
title('Linear Fit for Given Data');
ques 4 t = 0,0.5,1,5,10,20 p =760,625,528,85,40,10.6
find out least square fit and plot
t = [0, 0.5, 1, 5, 10, 20];
p = [760, 625, 528, 85, 40, 10.6];
model = @(x, t) x(1) * exp(x(2) * t);
x0 = [760, -0.1];
x = lsqcurvefit(model, x0, t, p);
p_fit = model(x, t);
figure;
plot(t, p, 'o');
hold on;
plot(t, p_fit, '-');
grid on;
xlabel('t');
ylabel('p');
title('Least Squares Fit');
Sarthak Lakhera 23119030 A18
Sarthak Lakhera 23119030 A18
Sarthak Lakhera 23119030 A18
Sarthak Lakhera 23119030 A18
Published with MATLAB® R2024b
Sarthak Lakhera 23119030 A18
SARTHAK LAKHERA 23119030
Experiment-3
Aim:
To understand and implement Interpolation & Data
analysis/statistics in matlab.
Software used:
MATLAB
In Lab Exercise:
Q1. Given X = {0…10} , Y = cos X , Xnew = {0….10} . Implement all three
methods of interpolation and plot them in one single figure,
distinguish between them using legends.
Solution code:
clc
close all
clear all
x = linspace(0,10,11);
y = cos (x);
xnew = linspace(0,10,1000);
stem(x,y)
hold on
ynew1 = interp1(x,y,xnew,'nearest');
plot(xnew,ynew1)
ynew2 = interp1(x,y,xnew,'linear');
plot(xnew,ynew2)
ynew3 = interp1(x,y,xnew,'spline');
plot(xnew,ynew3)
hold off
title("Comparision between diffferent techniques of interpolation")
grid on
legend("original","nearest","linear","spline")
SARTHAK LAKHERA 23119030
SARTHAK LAKHERA 23119030
Q2. Given X = [0 , 0.785 , 1.570 , 2.536 , 3.141 , 3.927 , 4.712 , 5.499 ,
6.283] , Y = [0 , 0.707 , 1.00 , 0.707 , 0 , -0.707 , -1.00 , - 0.707 , -0] .
Implement all three methods of interpolation and plot them in one
single figure, distinguish between them using legends.
Solution code:
clc
close all
clear all
x = [0 0.785 1.570 2.536 3.141 3.927 4.712 5.497 6.283]
y = [0.00 0.707 1.00 0.707 0.00 -0.707 -1.00 -0.707 -0.00]
xnew = linspace(0,6.3,1000);
stem(x,y)
hold on
ynew1 = interp1(x,y,xnew,'nearest');
SARTHAK LAKHERA 23119030
SARTHAK LAKHERA 23119030
plot(xnew,ynew1)
ynew2 = interp1(x,y,xnew,'linear');
plot(xnew,ynew2)
SARTHAK LAKHERA 23119030
SARTHAK LAKHERA 23119030
ynew3 = interp1(x,y,xnew,'spline');
plot(xnew,ynew3)
hold off
title("Comparision between diffferent techniques of interpolation")
grid on
legend("original","nearest","linear","spline")
Learning Outcome:
● Learnt about interpolation techniques and implemented them.
● Learnt about the implementation of hold function and legend
function.
SARTHAK LAKHERA 23119030
SARTHAK LAKHERA 23119030
SARTHAK LAKHERA 23119030