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

조회 수: 5 (최근 30일)
Daniel
Daniel 2019년 7월 2일
댓글: Daniel 2019년 7월 3일
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?

채택된 답변

Steven Lord
Steven Lord 2019년 7월 2일
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
Steven Lord
Steven Lord 2019년 7월 3일
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 2019년 7월 3일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by