0% found this document useful (0 votes)
25 views5 pages

Lab 5

1. The document describes MATLAB code for different interpolation methods, including linear interpolation using interp1() and interp2(), and cubic spline interpolation using spline(). 2. Linear interpolation is applied to a sine function and temperature/EMF data, while spline interpolation fits a curve through 5 known sin(x) points. 3. Interpolation functions like interp1(), interp2() and spline() are useful for estimating values between known data points and constructing smooth curves from discrete measurements.

Uploaded by

Kashif hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Lab 5

1. The document describes MATLAB code for different interpolation methods, including linear interpolation using interp1() and interp2(), and cubic spline interpolation using spline(). 2. Linear interpolation is applied to a sine function and temperature/EMF data, while spline interpolation fits a curve through 5 known sin(x) points. 3. Interpolation functions like interp1(), interp2() and spline() are useful for estimating values between known data points and constructing smooth curves from discrete measurements.

Uploaded by

Kashif hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Date: DEC-12-2023

LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No:05 Roll No:CH-20313
LAB ASSESSMENT RUBRICS

TITLE: Interpolation and Curve fitting.


DESCRIPTION:

Question 1: linear interpolation for sine function.


CODES:
1. m. file:
close all; clear all;
ezplot('sin(x)',[0 10]); hold on; % plot sin(x) forinterval 0~10.
x = 0:10; y = sin(x); % make given data x, y
xc = 0:.25:10; % make points forinterpolation
yc = interp1(x,y,xc); % for x and y, then calc and save yc for xc
plot(x,y,'o',xc,yc,'.-');
legend('original sine curve','given points','1storder interpolation','3')
2. command window:
Date: DEC-12-2023
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No:05 Roll No:CH-20313

Question 2: Plot the following data of temperatures and electromotive


force(emf) of thermocouples with 'bo-'. Show the values using linear
interpolation with 'g-.' which interval is 1.Then, for temperatures of 52 and
77, show the calculated emf using linear interpolation with red*.

temperatures(℃) 40 48 56 64 72 80
emf(mV) 1.33 1.67 2.08 2.36 2.71 3.19
CODES:
1)m. file:
%prob_2 example of linear interpolation
clear all; close all; clc
x=[40 48 56 64 72 80];
y=[1.33 1.67 2.08 2.36 2.71 3.19];
xx=[40:80]; yy=interp1(x,y, xx);
plot(x,y,'bo-',xx,yy,'g.-'); hold on; xlabel('Temp(degree)');
ylabel('emf(mV)');
x2=[52 77]; y2=interp1(x,y, x2);
plot(x2,y2,'r*')
legend('given points','1st interpolation','1st interpolationgiven points','2');

2) COMMAND WINDOW:
Date: DEC-12-2023
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No:05 Roll No:CH-20313

Task 2: Linear interpolation for two independent variables


The MATLAB function zc=interp2(x,y,z,x0,y0) interpolates to find zc, the values of the
underlying.2-D function z at the points in matrices x0 and y0. Matrices x and y specify the points
at which the data z is given.
Question 3: Next table is the measured temperatures at 0 to 5 seconds for rpm of a engine.
1. draw the data in 3 dimensional using surf(). 2. printout as belows. 3. calculate the temperature
for a given time and rpm(use input() function) which is made by function interp2(), but finish that
time is 0(use while ... end).

CODES:
1)m. file:
%prob_3 linear interpolation with two independent v.
clear all; close all;
x=[0 1 2 3 4 5]; % 1x6
y=[2000 3000 4000 5000 6000]; % 1x5
z=[ 0 0 0 0 0
20 110 176 190 240
60 180 220 285 327
68 240 349 380 428
77 310 450 510 620
110 405 503 623 785] % 6x5 matrix.
surf(x,y,z'); % or surf(x',y',z)
xlabel('Time(sec)'); ylabel('Speed(rpm)');
zlabel('Temperature(Degree)');
fprintf('Table 1. Engine Temperature variation for time(sec) vs.rpm.\n')
fprintf('time, rpm :'); fprintf('%6d',y); fprintf('\n');
for i=1:length(x);
fprintf('%3d ',x(i)); fprintf('%6d',z(i,:));
fprintf('\n');
end
x1=input('time=');
while x1 ~= 0;
y1=input('rpm='); z1=interp2(x,y,z',x1,y1);
fprintf('t=%3.1fsec, rpm=%4d RMP,temp= %6.2f(C)\n',x1,y1,z1);
x1=input('time=');
end
2) COMMAND WINDOW:
Date: DEC-12-2023
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No:05 Roll No:CH-20313

Task 3: Spline interpolation


In Matlab, cubic spline associated functions are pp=spline(x,y) calculate coefficients of pp
provides the piecewise polynomial form of the cubic spline interpolant to the data values y at the
data sites x. yy=ppval(pp,xx) calculate the spline results yy at xx with the coefficients pp.
yy=spline(x,y,xx) is the same as yy=ppval(spline(x,y),xx) General usage is the calculation that
absence of end slopes. But, if number of y data is two more x, the both ends are recognized as
slopes. ex [1 y 1]
Question 4: Example of Cubic Spline
sin(x) values are given for 5 points with the same interval from 0 to 2pi, compare the true sine
values and the spline values (absence the slopes and present the slopes), where, spline coefficients
calculate with 5 points, the number of calculation point of x is 21.

CODES:
1)m. file:
%prob_4.m Example of Cubic Spline
clear all; close all; clc;
x = 0: 2*pi/4: 2*pi; y = sin(x); % make 5 points of x; calc y with sin(x);
Date: DEC-12-2023
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No:05 Roll No:CH-20313
plot(x, y,'or'); hold on; % draw given 5 points with plot( ,'o'); hold on;
ezplot('sin(x)',[0 2*pi]);% draw sine curve with ezplot(); %interval 0 to 2pi.
xx = [0: 2*pi/20: 2*pi]; % make 21 points of xx; % increment 2*pi/20
yy = spline(x,y,xx); % calculate yy with spline; % (withoutboth ends slope)
plot(xx, yy,'-c.') % draw calculated values with plot( ,'-c.');
y2=spline(x,[1 y 1],xx); % both ends slope is all 1 at x=0,2pi. dsin(x)/dx = cos(x)
plot(xx,y2,'-m.') ; % draw y2 with plot( ,'-r.');
legend('given point','original sine curve','without slope','with slope','1')
red points and line are spline values with both ends slope. (itis nearly same to true
curve ?!)
2) COMMAND WINDOW:

RESULT & DISCUSSION:


Utilizing MATLAB, linear interpolation for the sine function was accomplished
with `interp1()`, while `interp2()` facilitated the same for two independent variables.
Spline interpolation, achieved through `spline()`, provided a smooth curve fitting
approach. These functions are essential for precise estimations and curve
constructions between known data points.

You might also like