필터 지우기
필터 지우기

PLEASE HELP with Forward, backward, and central difference approximations?

조회 수: 5 (최근 30일)
equinox
equinox 2017년 2월 5일
답변: Image Analyst 2017년 2월 5일
Hi I am really confused on how to approach this problem, I have started a little but am really not sure where to go from here so any help would be appreciated.
Consider the function f(x) =(1+ x^2 )^-1 defined on [-5, 5] . Choose equally-spaced grid points xi = -5+ih, where i=0,..,n and h=10/n with n= 11,21,51,101.
Approximate f'(xi) for i=1,..,n-1 using forward difference, backward difference, central difference approximations. Plot the results.
So far this is what i have:
f = @(x) ((1+x.^2).^(-1));
fprime = @(x) (-2*x)./((1+x.^2).^(2));
n=11;21;51;101;
h=10/n;
for i=1:n+1
x(i)=-5+(i-1)*h
end
x=linspace(-5,5,x(i));

답변 (1개)

Image Analyst
Image Analyst 2017년 2월 5일
You need brackets around the list of numbers where you define n. And you need to have the computation of h in a loop. See if this makes sense:
f = @(x) ((1+x.^2).^(-1));
fprime = @(x) (-2*x)./((1+x.^2).^(2));
n = [11;21;51;101]
h = 10 ./ n
for nIndex = 1 : length(n)
this_n = n(nIndex)
% for this value of n...
% Compute this value of h...
this_h = 10 / this_n
% Now loop over i to compute x...
for i = 1 : this_n
x(i) = -5 + i * this_h
end
end
There are ways to do it without loops (using meshgrid) but they might be too confusing to you so I'm doing the simple, intuitive for loop way.

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by