Help with basics and finite difference method
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have to write a program using the finite-difference formula to calculate the approximate value for the derivative of a function. The test will be tan(x) for x=1, determining the error by comparing with sec^2(x). I have no idea where to begin.
採用された回答
Image Analyst
2014 年 9 月 14 日
How about a for loop and taking the delta Y over the delta X where the separation is decreasing until it gets really really small, then compare to sec^2(x) and see how the difference gets smaller and smaller as the separation gets smaller and smaller. That's the finite difference method.
7 件のコメント
Will
2014 年 9 月 14 日
I'm supposed to use a log scale for the h (as in f(x+h)) and for the magnitude of error, would that get in the way of the for loop?
Thank you so much. I feel helpless.
Image Analyst
2014 年 9 月 14 日
編集済み: Image Analyst
2014 年 9 月 14 日
So just start with h = 1, then go to h = 0.1, then to h = 0.01, then to h = 0.001, etc. You'll make h get smaller logarithmically.
x = 1;
for k = 1 : 6
h = 10^(1-k);
leftYValue = tan(x-h);
rightYValue = tan(x+h);
slope = ................
and so on.
Will
2014 年 9 月 14 日
Oh my god, I thought I was going to have to figure out how to actually implement f'(x) = (f(x+h)-f(x))/h. Numerical analysis is so foreign... sheesh. Well thank you so much, I appreciate it immensely.
Image Analyst
2014 年 9 月 14 日
Well that is what you're implementing. In my code, I went h above and h below the target x value (because I wanted to be symmetric), not just to one side like your equation, so my denominator would be 2*h rather than h. And my leftYValue is your f(x) and my rightYValue is your f(x+h). So the slope is (rightYValue - leftYValue)/(2*h), which is essentially your equation.
Will
2014 年 9 月 15 日
I see, that actually seems to be better than what is suggested by the instructor... I'll ask if I can try something like that. So I use that, and then compare by subtracting the slope from sec^2(x), and then plot the magnitude of error from that subtraction on one axis, and h on the other. That is so much better! Thank you!
Image Analyst
2014 年 9 月 15 日
OK, great, glad I could help. Can you go ahead and mark the answer as Accepted then?
Will
2014 年 9 月 15 日
I will, but I just talked to my instructor and he explained that though your method is valid, I must use the given formula. Could I just do the same things we talked about, only replace slope with
slope = (tan(x+h)-tan(x))/h
and do the error vs. h plot? I'm going to use loglog(h, error, -s), but this gives me a weird window... Please let me know if you can help with that as well, though I understand if you don't want to. Thanks for helping!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
タグ
参考
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)
