I keep getting ode45 (line 115) and odearguments (line 90) errors

1 Ansicht (letzte 30 Tage)
Daniel
Daniel am 2 Jul. 2019
Kommentiert: Daniel am 3 Jul. 2019
This code was given to me by my professor for a dynamics project.
function [t,x,fN]=lab2(x0,f0,om)
m = 2;
L = 0.6;
NPERD = 250;
npt_PERD = 200;
dt = 2*pi/om/npt_PERD;
tspan = dt*(1:NPERD*npt_PERD);
N = length(tspan);
opt = odeset('RelTol',1e-10,'AbsTol',1e-9);
[t,x] = ode45(@bar,tspan,x0,opt,m,L,f0,om);
function G = bar(t,x,m,L,f0,om)
% x(1): position of wheel (point A)
% x(2): velocity of wheel
% x(3): angle of the bar
% x(4): angular velocity of the bar
G(1,1)=x(2);
G(3,1)=x(4);
A = [m 0.5*m*L*cos(x(3));0.5*m*L*cos(x(3)) m*L^2/3];
rhs(1,1)= 0.5*m*L*(x(4))^2*sin(x(3))+f0*cos(om*t); % enter p function on the RHS of EOM
rhs(2,1)= -0.5*m*g*L*sin(x(3)); % enter q function on the RHS of EOM
G([2 4],1)=A\rhs;
I was only required to determine the two equations for rhs(1,1) and rhs(2,1) and plug them into the code. However, I'm getting several errors and I just don't understand much of what he's written for the program. These are the errors I'm getting:
>> lab2(0,5,25*pi)
Index exceeds array bounds.
Error in lab2>bar (line 19)
G(1,1)=x(2);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in lab2 (line 11)
[t,x] = ode45(@bar,tspan,x0,opt,m,L,f0,om);
What am I doing wrong?

Akzeptierte Antwort

Steven Lord
Steven Lord am 2 Jul. 2019
Your x0 input to lab2 is a scalar so when you passed that into ode45 that tells ode45 that you have one initial condition. Because of that, you're solving a system of one ODE. There is no second element of the x vector with which ode45 calls your bar function, so when you try to ask for the value of that second element MATLAB correctly throws an error.
Your wheel starts off at position 0 at time 0. What's its initial velocity at that time? What are the initial angle and angular velocity of the bar at that time? Specify those in x0.
  4 Kommentare
Steven Lord
Steven Lord am 3 Jul. 2019
There was a comment by Daniel (since deleted) between my answer and my comment remarking that I'd correctly identified the problem and asking how to plot some aspects of the solution.
Daniel, please don't delete comments when doing so would make the flow of the conversation discontinuous as in this case (my comment talking about plots seems a non sequiter without the comment.)
Daniel
Daniel am 3 Jul. 2019
My apologies. I had deleted it since I was able to get the plots. I didn't refresh the page first to see if there was a response before doing so. Won't happen again

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming 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