plotting leastcurve fit through exceldata

2 views (last 30 days)
i want to develop a code in which i have to fit a curve using least curve method ,here i have to import the data from excel sheet. from the excel sheet i have to take first 5 points from 1 row 1 column and fit a curve now the again the next 5 points from 2 row 1 column upto n times and fit a curve
i have written for the first 5 points and its working fine. now when i have to slide it to next five points i have given for loop for it and upto that i am getting correct but then i am not getting how to do it further
here is the code for the first 5 points
%% import the data points
function phi = curvefit3(x,y)
[data, ~] = xlsread('accn_rate_ekf01.xlsx');
x_vec = data((1:5),1);
y_vec = data((1:5),2);
x0 = x_vec(01);
N = length(x_vec);
%% construction of the least-squares quadratic fit to the data
%% we use the equation y =a0 + a1t + a2t^2
%% numbers a0,a1 and a2 are the unknowns
x_mat = [ ones(N,1) x_vec x_vec.^2 ];
amat = x_mat'*x_mat;
bmat = x_mat'*y_vec;
phi = inv(amat)*bmat;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
[xfit,yfit] = func(phi,x_vec);
%fun = @a2*(x_vec-x0).^2+a1*(x_vec-x0)+a0;
%yfit = fun(x);
figure;
plot(x_vec,y_vec,'or',xfit ,yfit,'-xg');hold on; grid;
return;
%
% calculation of function
%
function [xout,yout] = func(phi,x_vec);
%
x0 = x_vec(01);
x1 = x_vec(01);
x2 = x_vec(05);
x_array= x1:0.01:x2;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
val= a2*(x_array-x0).^2+a1*(x_array-x0)+a0;
xout= x_array;
yout = val;
return;
now plzz help me do the same for next five upto n rows
i am not getting how to do it

Accepted Answer

sanket neharkar
sanket neharkar on 17 May 2022
i tried and had did this and i am getting expected outcomes ;
can anyone suggest any short method....
function phi = curvefit3(x,y)
clear all;
close all;
[data, ~] = xlsread('filename.xlsx');
x_imp = data((1:n),1); %%reading rows and first column
y_imp = data((1:2n),2); %%reading rows and second column
%figure;
[rows, ~] = size(data);
for i = 1: 1 : n-5 %%loop to read i+1 to i+5 rows
x = data((i+1:i+5),1);
y = data((i+1:i+5),2);
x0 = x(1);
var1 = x - x0;
var2 = var1.*var1;
%% construction of the least-squares quadratic fit to the data
%% we use the equation y =a0 + a1t + a2t^2
%% numbers a0,a1 and a2 are the unknowns
x_mat = [ones(5,1) var1 var2];
amat = x_mat'*x_mat;
bmat = x_mat'*y;
phi = inv(amat)*bmat;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
[xfit,yfit] = func(phi,x);
figure;
plot(x,y,'or',xfit,yfit,'-xg');
%hold on;
%hold on
%plot(xfit ,yfit,'-xg'); grid;
end %for i = 1: 1 : n-5
return;
%
% calculation of function
%
function [xout,yout] = func(phi,x);
%
x0 = x(01);
x1 = x(01);
x2 = x(05);
x_array= x1:0.01:x2;
a0 = phi(1);
a1 = phi(2);
a2 = phi(3);
val = a2*(x_array-x0).^2+a1*(x_array-x0)+a0;
xout = x_array;
yout = val;
return;
this is what i was expecting

More Answers (1)

dpb
dpb on 16 May 2022
Pass the data and generalize instead; don't hard code indices into indexing expressions. Also, MATLAB has builtin functions polyfit, polyval use them; they're much more efficient and stable numerically than inverting the design matrix.
% driver
data=readmatrix('accn_rate_ekf01.xlsx'); % xlsread() is deprecated
% function
function [b,yhat,xo,yo] = curvefit3(data,nPts) % let set number points
if nargin<2, nPts=5; end % default is 5
nM1=nPts-1; % convenient temp
nR=size(data,1); % number rows
i1=1; % starting index
for i=1:nR-nM1 % for how many fits there are
i2=i1+nM1; % end of section
x=data(i1:i2,1); y=data(i1:i2,2); % just a temporary for shorter typing
b{i}=polyfit(x,y,2); % compute coefficients, save as cell array
yhat{i}=polyval(phi{i},x); % save the predicted
xo{i}=x; yo{i}=y; % save the section x,y data as alternate outputs
end
end
Call the function from your main program; I'd recomend plots be done there, too, not in the computing function
  1 Comment
sanket neharkar
sanket neharkar on 17 May 2022
can you help me with the plots too
i am not getting it

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!