Help with basics and finite difference method
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
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!
OK, great, glad I could help. Can you go ahead and mark the answer as Accepted then?
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
