How to define a special function with some points
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I want to define a function like this
f(0.2)=1.42007;
f(0.4)=1.88124;
f(0.5)=2.12815;
f(0.6)=2.38676;
f(0.7)=2.65797;
f(0.8)=3.94289;
f(1)=3.55975;
to use these values in a For Loop. How can I define function f?
Thanks in advance.
採用された回答
Ameer Hamza
2020 年 10 月 10 日
編集済み: Ameer Hamza
2020 年 10 月 10 日
You can use interp1()
x = [0.2 0.4 0.5 0.6 0.7 0.8 1];
y = [1.42007 1.88124 2.12815 2.38676 2.65797 3.94289 3.55975];
f = @(xq) interp1(x, y, xq);
Then you can also evaluate in for in-between points
>> f(0.2)
ans =
1.4201
>> f(0.3)
ans =
1.6507
>> f(0.55)
ans =
2.2575
6 件のコメント
Mojtaba Mohareri
2020 年 10 月 10 日
編集済み: Mojtaba Mohareri
2020 年 10 月 10 日
Thank you very much.
This is my code
clear all;
clc;
x = [0.2 0.4 0.5 0.6 0.7 0.8 1];
y = [1.42007 1.88124 2.12815 2.38676 2.65797 3.94289 3.55975];
f = @(xq) interp1(x, y, xq);
x = 0.6;
h = 0.4;
D(1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2)
for i=1:2
h = h/2;
D(i + 1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2);
for j=1:i
D(i + 1,j + 1) = (4^j*D(i + 1,j) - D(i,j))/(4^j - 1)
end
end
But the output is as follows
D =
NaN 0 0
26.2652 NaN 0
1.2600 -7.0751 NaN
Could you please tell me what the "NaN" is?
Mojtaba Mohareri
2020 年 10 月 10 日
When I compute (f(x + h) -2*f(x)+ f(x - h))/(h^2) in Command Window separately, it equels to 1.2600, but actually D(1,1)=(f(x + h) -2*f(x)+ f(x - h))/(h^2) which is NaN.
clear all;
clc;
X = [0.2 0.4 0.5 0.6 0.7 0.8 1];
y = [1.42007 1.88124 2.12815 2.38676 2.65797 3.94289 3.55975];
f = @(xq) interp1(X, y, xq, 'linear', 'extrap');
x = 0.6;
h = 0.4;
f(x+h)
ans = 3.5598
f(x)
ans = 2.3868
x-h
ans = 0.2000
(x-h) - X(1)
ans = -5.5511e-17
f(x-h)
ans = 1.4201
D(1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2)
D = 1.2894
for i=1:2
h = h/2;
D(i + 1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2)
for j=1:i
D(i + 1,j + 1) = (4^j*D(i + 1,j) - D(i,j))/(4^j - 1)
end
end
D = 2×1
1.2894
26.2652
●
D = 2×2
1.2894 0
26.2652 34.5905
●
D = 3×2
1.2894 0
26.2652 34.5905
1.2600 0
●
D = 3×2
1.2894 0
26.2652 34.5905
1.2600 -7.0751
●
D = 3×3
1.2894 0 0
26.2652 34.5905 0
1.2600 -7.0751 -9.8528
●
Walter Roberson
2020 年 10 月 10 日
If you look closely at this, you will see that 0.6 - 0.4 is not equal to 0.2 and instead is slightly less. Because of that, interp1() considered you to be doing extrapolation and the default extrapolation is NaN.
Ameer Hamza
2020 年 10 月 10 日
It happens when the input xq goes beyond the range of values in x. In your case, if xq is less than 0.2 or higher than 1.0, interp1 will give NaN. To avoid this, use extrapolation.
clear all;
clc;
x = [0.2 0.4 0.5 0.6 0.7 0.8 1];
y = [1.42007 1.88124 2.12815 2.38676 2.65797 3.94289 3.55975];
f = @(xq) interp1(x, y, xq, 'linear', 'extrap');
x = 0.6;
h = 0.4;
D(1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2)
for i=1:2
h = h/2;
D(i + 1,1) = (f(x + h) -2*f(x)+ f(x - h))/(h^2);
for j=1:i
D(i + 1,j + 1) = (4^j*D(i + 1,j) - D(i,j))/(4^j - 1)
end
end
Mojtaba Mohareri
2020 年 10 月 10 日
編集済み: Mojtaba Mohareri
2020 年 10 月 10 日
I understood. It works properly. Thank you so much for your consideration.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Surface and Mesh Plots についてさらに検索
タグ
参考
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)
