Please answer this in Matlab. I have posted a link to the question. The data tha
ID: 3802303 • Letter: P
Question
Please answer this in Matlab.
I have posted a link to the question. The data that is loaded is set of points which creates a parabola. I have posted the code that I am working with below. The code I have creates a line of best fit for a linear lsa, I need a parabola. Thank You
function LSA
load('Lab 8 data2.mat')
[m,n] = size(data2);
if n~=2
error('data2 is not 2-dimesnional')
end
A = ones(m,2);
A(:,1) = data2(:,1);
b = data2(:,2);
x = (A'*A)(A'*b);
C = x(1); D= x(2);
tleft = min(data2(:,1)); tright = max(data2(:,1));
t = linspace(tleft, tright, 100);
y = C.*t + D;
plot(data2(:,1),data2(:,2),'r*',t,y,'k')
legend('data2','line of best fit','Location','Northwest')
fprintf('The line of best fit is y = %ft + %f. ', C, D)
end
Explanation / Answer
Matlab code
load('Lab 8 data2.mat');
[m,n] = size(data2);
if n~=2
error('data2 is not 2-dimesnional')
end
V = fliplr(vander(data2(:,1))); %Vandermonde matrix
A = V(:,1:3); % Seperating the first three column (we need a polynomial of degree two)
b = data2(:,2);
x = A;
C = x(3); D= x(2); E = x(1);
tleft = min(data2(:,1)); tright = max(data2(:,1));
t = linspace(tleft, tright, 100);
y = C.*t.^2 + D.*t + E;
plot(data2(:,1),data2(:,2),'r*',t,y,'k')
legend('data2','parabola fit','Location','Northwest')
fprintf('The line of best fit is y = %ft.^2 + %ft+%f. ', C, D,E)