Forward, central, backward difference

15 Ansichten (letzte 30 Tage)
Heather Statt
Heather Statt am 7 Feb. 2018
Beantwortet: Heather Statt am 7 Feb. 2018
I am struggling making this code work. I have to show For the initial velocity of 25 m/s and kick angle of 40 plot the trajectory of the ball. The finite difference method (forward, backward, and central finite difference)need to be used to approximate the derivative of an equation
Estimate the value of the first derivative using the forward, backward and central finite difference
Plot the approximated values from each method on the same plot once along horizontal direction x and once along vertical direction y for the kick angle of 40. Label the plot and discuss the error. code is:
theta0=40; v0=25; deltat=0.01; [x,y,t]=projectile_simple(v0, theta0, deltat);
%%finite diference %forward Vx_f=zeros(1,length(x)); Vy_f=zeros(1,length(x));
for jj=1:length(x)-1 Vx_f(jj)=(x(jj+1)-x(jj))/deltat; Vy_f(jj)=(y(jj+1)-y(jj))/deltat; end
%need correction for last index Vx_f(end)=Vx_f(end-1); Vy_f(end)=Vy_f(end-1); figure hold on plot(t,Vy_f)
%% % %backward Vx_b=zeros(1,length(x)); Vy_b=zeros(1,length(x));
for k=2:length(x) Vx_b(k)=(x(k-1)-x(k))/-deltat; Vy_b(k)=(y(k-1)-y(k))/-deltat; end
%need correction for last index Vx_b(1)=Vx_b(2); Vy_b(1)=Vy_b(2); %deltat=x(2)-x(1);
plot(t,Vy_b) % %% %central Vx_c=zeros(1,length(x)); Vy_c=zeros(1,length(x));
for m=2:length(x)-1 Vx_c(m)=((x(m+1)-x(m-1)))/(2*deltat); Vy_c(m)=((y(m+1)-x(m-1)))/(2*deltat); end
%need correction for last index Vx_c(1)=Vx_c(2); Vy_c(1)=Vy_c(2);
Vx_c(end)=Vx_c(end-1); Vy_c(end)=Vy_c(end-1); deltat=x(2)-x(1);
plot(t,Vy_c)

Antworten (1)

Heather Statt
Heather Statt am 7 Feb. 2018
here's the code:
theta0=40;
v0=25;
deltat=0.01;
[x,y,t]=projectile_simple(v0, theta0, deltat);
%%finite diference
%forward
Vx_f=zeros(1,length(x));
Vy_f=zeros(1,length(x));
for jj=1:length(x)-1
Vx_f(jj)=(x(jj+1)-x(jj))/deltat;
Vy_f(jj)=(y(jj+1)-y(jj))/deltat;
end
%need correction for last index
Vx_f(end)=Vx_f(end-1);
Vy_f(end)=Vy_f(end-1);
figure
hold on
plot(t,Vy_f)
%backward
Vx_b=zeros(1,length(x));
Vy_b=zeros(1,length(x));
for k=2:length(x)
Vx_b(k)=(x(k-1)-x(k))/-deltat;
Vy_b(k)=(y(k-1)-y(k))/-deltat;
end
%need correction for last index
Vx_b(1)=Vx_b(2);
Vy_b(1)=Vy_b(2);
%deltat=x(2)-x(1);
plot(t,Vy_b)
%
%central
Vx_c=zeros(1,length(x));
Vy_c=zeros(1,length(x));
for m=2:length(x)-1
Vx_c(m)=((x(m+1)-x(m-1)))/(2*deltat);
Vy_c(m)=((y(m+1)-x(m-1)))/(2*deltat);
end
%need correction for last index
Vx_c(1)=Vx_c(2);
Vy_c(1)=Vy_c(2);
Vx_c(end)=Vx_c(end-1);
Vy_c(end)=Vy_c(end-1);
deltat=x(2)-x(1);
plot(t,Vy_c)

Kategorien

Mehr zu Graphics Objects 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!

Translated by