Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Need help debugging code. Will not plot data

5 visualizaciones (últimos 30 días)
David Geistlinger
David Geistlinger el 17 de Abr. de 2019
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Main File
%% define parameters related to road shape
road_params.b = 2.0;
road_params.c = 0.3;
road_params.d = 1.5;
road_params.e = 1.0;
%% define parameters related to car
car_params.mb = 1200.0;
car_params.mt = 15.0;
car_params.ks = 30000.0;
car_params.kt = 100000.0;
car_params.cs = 3000.0;
car_params.v = 5.0;
%% generate the time interval of solution
t = linspace(0, 4.0, 1000);
%% solve the system using ode45
x_init = [0, 0, 0, 0]';
[t, x] = ode45(@(t, x)half_car_system(t, x, road_params, car_params), t, x_init);
%% plot the solution
road = [];
for i=1:1:1000
road = [road, road_shape(t(i), road_params, car_params.v)];
end
%road = road_shape(t, road_params, car_params.v);
plot(t, road, 'b-;road;', 'linewidth', 2, t, x(:,1), 'k-;xt;', 'linewidth', 1.5, t, x(:, 3), 'g-;xb;', 'linewidth', 1.5);
xlabel('time (s)');
ylabel('response (m)');
title('half car system response');
Output function
function [dxdt] = half_car_system(t, x, road_params, car_params)
y = road_shape(t, road_params, car_params.v);
dxdt = zeros(4,1);
dxdt(1,1) = x(2);
dxdt(2,1) = -x(1) + y;
dxdt(2,1) = dxdt(2,1)*car_params.kt/car_params.mt;
dxdt(3,1) = x(4);
dxdt(4,1) = (x(1) - x(3))*car_params.ks + (x(2) - x(4))*car_params.cs;
dxdt(4,1) = dxdt(4,1)/car_params.mb;
end
Input function
function [y] = road_shape(t,road_params,v)
if (t >= 0 && t < road_params.e/v)
y = 0;
%end
elseif (t >= road_params.e/v && t < (road_params.e + road_params.d)/v)
y = road_params.c*sin(pi*v*(t - road_params.e/v)/road_params.d);
%end
elseif (t >= (road_params.e + road_params.d)/v && t < (road_params.e + road_params.d + road_params.b)/v)
y = 0;
%end
elseif (t >= (road_params.e + road_params.d + road_params.b)/v && t < (road_params.e + 2*road_params.d + road_params.b)/v)
y = road_params.c*sin(pi*v*(t - (road_params.e + road_params.d + road_params.b)/v)/road_params.d);
else
y = 0;
end
% z1 = ones(size(t));
% z1(~(t >= road_params.e/v || t < (road_params.e + road_params.d)/v)) = 0.0;
% z2 = t;
% z2(~(z2 >= (road_params.e + road_params.d + road_params.b)/v | z2 < (road_params.e + 2*road_params.d + road_params.b)/v)) = 0.0;
% y = road_params.c*(sin(pi*v*z1/road_params.d) + sin(pi*v*z2/road_params.d));
end
I am receiving this error:
ME171ProjectODE
Error using plot
Invalid data argument.
Error in ME171ProjectODE (line 25)
plot(t, road, 'b-;road;', 'linewidth', 2, t, x(:,1), 'k-;xt;', 'linewidth', 1.5, t, x(:,
3), 'g-;xb;', 'linewidth', 1.5);
I have all 3 files in same folder not sure why it is giving me an error message on the plotting portion of this.
  1 comentario
Walter Roberson
Walter Roberson el 17 de Abr. de 2019
What do you intend your plot argument 'b-;road' to mean ? What are you intending the 'k-;xt;' argument to mean?
My guess would be that you are trying so specify legend entries for the plots. You cannot do that as part of a linespec argument. There is a 'DisplayName' option for plot(), but only one of them will be paid attention to in any one call to plot()

Respuestas (0)

La pregunta está cerrada.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by