Project

CURVE FITTING FOR THE GIVEN DATA IN MATLAB

Aim:

To do the curve fit using matlab

Objective:

  • program to run the curvefit in matlab

Theory:

Curvefitting is the process of constructing a curve, or mathematical funcation. Most of the time, the curve fit will produce an equation that can be used to find points anywhere along the curve. In some cases, you may not be concerned about finding an equation. Instead, you may just want to use a curve fit to smooth the data and improve the appearance of your plot.

The first degree polynomial equation

is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.

If the order of the equation is increased to a second degree polynomial, the following results:

This will exactly fit a simple curve to three points.

If the order of the equation is increased to a third degree polynomial, the following is obtained:

This will exactly fit four points.

A more general statement would be to say it will exactly fit four constraints. Each constraint can be a point, angle, or curvature . Angle and curvature constraints are most often added to the ends of a curve, and in such cases are called end conditions. Identical end conditions are frequently used to ensure a smooth transition between polynomial curves contained within a single spline. Higher-order constraints, such as "the change in the rate of curvature", could also be added.

Errors to find in curvefit:

The error of a genearal "i"point can be given by the funcation,

Error=`|y(i)-`value of `f(x)` at `x(i)|=|y(i)-f(x(i))|`

Programing part:

'data' is the name of the file saved in the compurter ,load is an inbuilt funcation which is used to call the data stored in it and here i an saving the data in the name of 'j' .

in name of temp i am going to save the first coloumn's data similarly the second coloum's as c_p

m is the value that how many values that we are going to brake the data

for loop is user to run the program for n number of times in this program it is going to run for i=(3200/400)=8 means, means for loop will run for 8times

curve fit is the funcation which we are calling to do the curvefit operation

clear all 
close all
clc
   % data source 
j=load('data')
temp=j(:,1)
c_p=j(:,2)

plot(temp,c_p)
hold on
m = 400;
n =0;
for i=1:(3200/m)
   te=j(n+1:n+m,1);
   cp=j(n+1:n+m,2);
    solve(n+1:n+m)=curve_fit(te,cp)
    n=n+m; 
end

plot(temp,solve)
pre=solve;

  %TO CALCULATE THE ERROR 
  % mean value of y axis terms 
mean_c_p= mean(c_p);

  % To determine SSR
error_1=solve-mean_c_p;
error_11=error_1.^2;
ssr=sum(error_11);

  % To determine SSE
s=pre';
error_2=c_p-s;
error_22=error_2.^2;
sse=sum(error_22);

  % To fing SST
sst=(ssr+sse);

  %To find the result
result=ssr/sst;

  %To find rmse
rmsc=(sse/length(temp))^.5;

xlabel('temperatur')
ylabel('specific heat')
legend('opiginal data','curve fit')

funcation curve_fit is used to do call the funcation in the main programe to do the polyfit and polyval operation for n numer of variables

polyfit syntex:ex

p = polyfit(x,y,7);
where the x indicates the x axis values
y indicates the y axis values
7th-degree polynomial to the points.

in the funcation we can change the value of polynomial as we required

polyval syntax:

polyval is going compair polyfit value with x axis values

y=polyval(p,x)

function [cur]=curve_fit(te,cp)

    polo=polyfit(te,cp,1);
    cur=polyval(polo,te);

end

Errors during programming:

to over come the error i have used another veriavle pre which helps me to transpose the coloums materix to row (solve )

pre=solve'

Results:

Result of curvefit in the linear polynomial for the Cp data.

`R^2` indicates the fitness of the curve

  • `R^2==(SSR)/(SST)=0.9999`

Results of curvefit in the cubic polynomial for the cp data.

  • `R^2=(SSR)/(SST)=1.0`
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.