fid = fopen(\'testdata2.dat\'); if fid == -1 disp(\'File open not successful\')
ID: 3696903 • Letter: F
Question
fid = fopen('testdata2.dat');
if fid == -1
disp('File open not successful')
else
% Creates x and y vectors for the data points
% Using fscanf
matData = fscanf(fid,'%f %f ', [2 inf]);
[fcurve, merror] = fLeastSquare(matData,1);
polycoef= double(coeffs(fcurve));
if polycoef(1)>0
strlinefunc=[num2str(polycoef(2)),' x + ',num2str(polycoef(1))];
else
strlinefunc=[num2str(polycoef(2)),' x - ',num2str((-1)*polycoef(1))];
end;
strError=[' Error : ', num2str(merror)];
width=1;
vX=min(matData(1,:))-width:0.1:max(matData(1,:))+width;
plot(matData(1,:),matData(2,:),'ro',vX,fcurve(vX),'k-' );
title([strlinefunc,strError]);
% Closing the file
closeresult = fclose(fid);
if closeresult == 0
disp('File close successful')
else
disp('File close not successful')
end
end
How could I modify this MATLAB code so is finds the line given by assuming that the x variable may contain the errors, and minimizing the sums of squares of the horizontal distances.
***Note fLeastSquare is defined by the following:
function [fcurve, merror] = fLeastSquare(data,degree)
%Data: matrix with two row of data(1,:) Xi and data(2,:) Yi list of points
%degree: degree of polynomial,the function we try to approximated.
numPts = length(data(1,:));
if numPts<degree
error('You need input more data.');
end;
syms x fcurve;
a=sym('a',[1,degree+1]);
% calculate sum of(f(xi)-yi)^2
distance=0;
for i=1:numPts
distexpr =0;
for j=1:degree+1
distexpr=a(j)*data(1,i)^(j-1)+distexpr;
end
distance = distance+(distexpr-data(2,i))^2;
end
%take derivative of each ci, find the minimun distance
for k =1:degree+1
lstEqns(k) = diff(distance, a(k)) == 0 ;
end;
soln=solve(lstEqns,a)
csoln=struct2cell(soln);
for i =1:length(csoln)
matSoln(i,:)=double(csoln{i})';
end;
[row,col]=size(matSoln);
%build curve function:c(n)*x^(n-1)+c(n-1)*x^(n-2)+...+c2*x+c1
merror=Inf;
for j=1:col
expr = 0;
for i =1:row
expr=matSoln(i,j)*x^(i-1)+expr;
end
f=symfun(expr,x);
merr = double(sum((f(data(1,:))- data(2,:)).^2)/(((f(1)-f(0))^2+1)*numPts));
if merr < merror
fcurve=f;
merror = merr;
end
end;
end
Explanation / Answer
numPts = length(data(1,:));
if numPts<degree
error('You need input more data.');
end;
syms x fcurve;
a=sym('a',[1,degree+1]);
% calculate sum of(f(xi)-yi)^2
distance=0;
for i=1:numPts
distexpr =0;
for j=1:degree+1
distexpr=a(j)*data(1,i)^(j-1)+distexpr;
end
distance = distance+(distexpr-data(2,i))^2;
end
%take derivative of each ci, find the minimun distance
for k =1:degree+1
lstEqns(k) = diff(distance, a(k)) == 0 ;
end;
soln=solve(lstEqns,a)
csoln=struct2cell(soln);
for i =1:length(csoln)
matSoln(i,:)=double(csoln{i})';
end;
[row,col]=size(matSoln);
%build curve function:c(n)*x^(n-1)+c(n-1)*x^(n-2)+...+c2*x+c1
merror=Inf;
for j=1:col
expr = 0;
for i =1:row
expr=matSoln(i,j)*x^(i-1)+expr;
end
f=symfun(expr,x);
merr = double(sum((f(data(1,:))- data(2,:)).^2)/(((f(1)-f(0))^2+1)*numPts));
if merr < merror
fcurve=f;
merror = merr;
end
end;
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.