I need help figuring out the mistake in my function approximation
이전 댓글 표시
I am doing function approximation but i'm not getting a smooth function at all. I've tried least squares method and backslash for the same question. Could someone please point out my mistake and help me? Thank you.
채택된 답변
추가 답변 (1개)
John D'Errico
2023년 10월 26일
편집: John D'Errico
2023년 10월 26일
Avoid the use of the normal equations as you used, i.e., this crapola that you called the least squares method. Backslash is just as much a least squares method!
% A = [sum(x.^2) sum(x.^3)
% sum(x.^3) sum(x.^4)];
% b = [sum(y.*x);sum(y.*x.^2)];
%
% % solution of the system
% c = inv(A)*b;
Properly solve for the coefficients using backslash.
x = [0.1 0.4 0.9 1.6 1.8]';
y = [0.4 1.0 1.8 4.1 5.5]';
Your model is: f(x) = c1x + c2x^2
A = [x,x.^2];
C12 = A\y;
plot(x,y,'o');
hold on
yfun = @(X) C12(1)*X + C12(2)*X.^2;
fplot(yfun,[min(x),max(x)])
Note my use of fplot there. Your evaluation of the points on the curve
% xx = x(1):0.1:x(end);
was a bad idea. Why? How many points did it generate? LOOK CAREFULLY! Just because 0.1 seems like a small number, IS IT REALLY????? What is x again?
x = [0.1 0.4 0.9 1.6 1.8]';
Alternatively, you might have used linspace to generate that vector. I would suggest your real problem was in just the use of a stride of 0.1 between points to evaluate the curve at.
카테고리
도움말 센터 및 File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


