Help... Getting errors with v_plot and x_plot

1 Ansicht (letzte 30 Tage)
Anthony Hill
Anthony Hill am 4 Mai 2018
Beantwortet: Wick am 4 Mai 2018
g = 9.81; m = 68.1; c = 0.25; h = 2; t = 0;
% Initial guess v_eu = 0; v_true = 0; x_eu = 350; x_true = 0; err_v = 100; err_x = 0;
i = 1; fprintf('Iter\t t\t\t x_true\t\t v_ture\t\t x_Euler\t v_Euler\t Error(x)\t Error(v)\n') while x_eu > -50 % Stopping criteria fprintf('%u\t %8.2f\t %8.2f\t%8.2f\t %8.2f\t %8.2f\t %8.2f\t%8.2f\n',i,t,x_true,v_true,x_eu,v_eu,err_x,err_v) slope = g-(c/m)*v_eu^2; % v' = slope
t = t+h; %Interval
x_true =(m/c)*log(cosh(t*sqrt(g*c/m)));
x_eu = x_eu - v_eu*h;
% Velocity
v_true = sqrt(m*g/c)*tanh(t*sqrt(g*c/m));
v_eu = v_eu + slope*h;
err_v = abs((v_true - v_eu)/v_true*100);
err_x = abs((x_true -(x_eu))/x_true*100);
*v_plot(1)=0;
v_plot(i+1)=v_eu;
**x_plot(1)=350;
x_plot(i+1)= x_eu*;*
*
i = i +1;
end t_plot=0:h:t; plot(t_plot,v_plot,t_plot,x_plot) title('(Problem 1(a) Euler Method') xlabel('Time (s)'); ylabel('V and X');

Akzeptierte Antwort

Wick
Wick am 4 Mai 2018
Your code was quite difficult to understand without proper formatting and extra '*' spread around. I cleaned up the lines and ran the following code and it ran just fine.
Because you're making your vectors larger in the loop you should really consider clearing them before you start the script. That way, the value of the vector from the run before doesn't influence the results of this run. I added a 'clearvars' to the start for that purpose. Otherwise, this seemed to work just fine.
clearvars
g = 9.81;
m = 68.1;
c = 0.25;
h = 2;
t = 0;
% Initial guess
v_eu = 0;
v_true = 0;
x_eu = 350;
x_true = 0;
err_v = 100;
err_x = 0;
i = 1; fprintf('Iter\t t\t\t x_true\t\t v_ture\t\t x_Euler\t v_Euler\t Error(x)\t Error(v)\n')
while x_eu > -50 % Stopping criteria
fprintf('%u\t %8.2f\t %8.2f\t%8.2f\t %8.2f\t %8.2f\t %8.2f\t%8.2f\n',i,t,x_true,v_true,x_eu,v_eu,err_x,err_v)
slope = g-(c/m)*v_eu^2; % v' = slope
t = t+h; %Interval
x_true =(m/c)*log(cosh(t*sqrt(g*c/m)));
x_eu = x_eu - v_eu*h;
% Velocity
v_true = sqrt(m*g/c)*tanh(t*sqrt(g*c/m));
v_eu = v_eu + slope*h;
err_v = abs((v_true - v_eu)/v_true*100);
err_x = abs((x_true -(x_eu))/x_true*100);
v_plot(1)=0;
v_plot(i+1)=v_eu;
x_plot(1)=350;
x_plot(i+1)= x_eu;
i = i +1;
end
t_plot=0:h:t;
plot(t_plot,v_plot,t_plot,x_plot)
title('(Problem 1(a) Euler Method')
xlabel('Time (s)');
ylabel('V and X');

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by