필터 지우기
필터 지우기

polynomial curve fitting error

조회 수: 5 (최근 30일)
Sumera Yamin
Sumera Yamin 2023년 4월 5일
댓글: Steven Lord 2023년 4월 6일
Hi i have a simple problem but strange error. am doing a polynomial curve fitting for two column vectors x and y. i use command "p = polyfit(x,y,3) . It gives me following error
Attempt to execute SCRIPT polyfit as a function:
What could be the possible reason for it. Many thanks

채택된 답변

John D'Errico
John D'Errico 2023년 4월 5일
편집: John D'Errico 2023년 4월 5일
Is there a good reason why you called the script you wrote polyfit?
Yes, I suppose you had a very good reason, because you wanted to write a script to use polyfit. But now consider what happens to MATLAB. Does it know what you intended when you then try to use polyfit. Which one should it use? The script, named polyfit, or the original function, also named polyfit? It tries to do its best, but it is confused. Never confuse a computer. It might get upset, and then go hack your checking account. ;-) These blasted computers sometimes can be vengeful things.
DO NOT NAME YOUR SCRIPTS OR YOUR OWN FUNCTIONS WITH THE NAMES OF EXISTING MATLAB FUNCTIONS. Do this at the command prompt:
whch polyfit -all
Then, rename your script.
  댓글 수: 3
John D'Errico
John D'Errico 2023년 4월 6일
편집: John D'Errico 2023년 4월 6일
How do you display them? I would probably put the coefficients into the title. Maybe like this:
x = rand(10,1);
y = exp(x);
P3 = polyfit(x,y,3)
P3 = 1×4
0.2736 0.4211 1.0228 0.9978
syms X
SP3 = dot(P3,X.^[3 2 1 0]);
plot(x,y,'o')
xlabel X
ylabel Y
title(char(vpa(SP3,3)))
hold on
fplot(SP3,[0,1])
Yes, I could have displayed the coefficients in the plot window itself. To do that, I might have used the text function, writing the text in one unused corner of the figure window. Or you don't need to used a symbolic expression at all, just put a list of numbers up. But I like the way syms does it.
Steven Lord
Steven Lord 2023년 4월 6일
You could also put it in the legend. I used sprintf to create a quick and slightly messy representation of the polynomial, but you could postprocess the string (removing a leading +, changing x^1 and x^0 to x and nothing respectively, omitting terms with a coefficient of 0, etc.)
x = rand(10,1);
y = exp(x);
n = 3;
P3 = polyfit(x,y,n)
P3 = 1×4
0.2895 0.4053 1.0245 0.9986
str = sprintf('%+g x^%d', [P3; n:-1:0])
str = '+0.289544 x^3+0.405343 x^2+1.02449 x^1+0.998551 x^0'
xs = sort(x);
plot(xs, polyval(P3, xs), 'o-', DisplayName=str)
legend show

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sam Chak
Sam Chak 2023년 4월 5일
If, for some reasons, you like to name your script "polyfit.m", try adding a prefix to make the filename unique.
Here are some examples:
  • myPolyFit.m
  • Test1_polyfit.m
  • Sumera_polyfit.m
Before saving the filename, you can check if the chosen filename exists in other MATLAB folders or not.
which myPolyFit -all
'myPolyFit' not found.
which PolyFit -all
/MATLAB/toolbox/matlab/polyfun/polyfit.m /MATLAB/toolbox/matlab/bigdata/@tall/polyfit.m % tall method /MATLAB/toolbox/parallel/gpu/@gpuArray/polyfit.m % gpuArray method /MATLAB/toolbox/parallel/parallel/@codistributed/polyfit.m % codistributed method
  댓글 수: 1
Sumera Yamin
Sumera Yamin 2023년 4월 5일
편집: Sumera Yamin 2023년 4월 5일
Many thanks. how do i display fitted coefficients on the x,y plot?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Fit Postprocessing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by