computing a interpolating polynomial for some function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am trying to understand how to use the commands polyfit and polyval so i am trying to interpolate some function, I wrote something so simple but even it is simple it gives me error
x=linspace(-1,1,20);
y= @(x) 1/(1+16*(x^2));
p = polyfit (x,y,2);
plot(x,p)
and this is the error message
Error using polyfit (line 48)
The first two inputs must have the same number of elements.
Error in polyfit_polyval (line 7)
p = polyfit (x,y,2);
採用された回答
x = linspace(-1,1,20);
yfunc = @(x) 1./(1+16*x.^2);
y = yfunc(x);
...
or
x=linspace(-1,1,20);
y=1./(1+16*x.^2);
...
9 件のコメント
so the problem was the "."?? because x is a vector
Torsten
2022 年 1 月 26 日
Yes, the dot after 1./ and the dot in x.^2.
Look up elementwise multiplication and division.
okay! thank you so much
now i tried what you said but it gives me error the same
x=linspace(-1,1,20);
y=1./(1+16*x.^2);
p = polyfit (x,y);
y1 = polyval(p,x);
plot(x,y,'o')
hold on
plot(x,y1,'-')
Error in polyfit_polyval (line 6)
p = polyfit (x,y);
Torsten
2022 年 1 月 26 日
You didn't specify the degree of the fitting polynomial.
okay my bad !!!!!! but why the graph looks like this

Torsten
2022 年 1 月 26 日
It's the best you can get for a quadratic polynomial as interpolant.
i understand now!! Thank you once again
In your original code, the main problem was that y was a function handle but polyfit requires its first two inputs to be numeric arrays of the same size. Note that Torsten defined the function not as y but as yfunc then evaluated yfunc with x as the input to generate y. So the data that got passed into polyfit as its second input was the result of evaluating that function at the points in x rather than the function.
The use of the element-wise operators allowed the function to be evaluated on an array and return an array with the same size. The matrix form of the operator would require the inputs to be square matrices.
A = [1 2; 3 4]
A = 2×2
1 2
3 4
y = A.*A % each element of y is the square of the corresponding element of A
y = 2×2
1 4
9 16
z = A*A % this perform matrix multiplication instead of element-wise
z = 2×2
7 10
15 22
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Interpolation についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
