PLEASE HELP with Forward, backward, and central difference approximations?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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));
0 Kommentare
Antworten (1)
Image Analyst
am 5 Feb. 2017
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!