my m-file looks like
function xp=F(t,x)
xp=zeros(5,1);
m=1;
w=1;
h=.5;
y=.01;
xp(1)=-m*(w^2)*(x(2)^3)*(-(h*y)/(2*w*m))*24*x(3);
xp(2)=x(1);
xp(3)=2*w*x(4);
xp(4)=-x(2)*w*(1+((12*x(2)*x(2))/(m*w*w)))*x(3)+w*x(5);
xp(5)=-x(2)*w(1+((12*x(2)*x(2))/(m*w*w)))*x(4);
and when i set m,w,y,and h then run the code
[t,x]=ode45('F',[0,10],[0,1,.5*((1+((12*x(2)*x(2))/(m*w*w)))^(-.5)),0,.5*((1+((12*x(2)*x(2))
/(m*w*w)))^(.5))]);
i get the errors
??? Index exceeds matrix dimensions.
Error in ==> F at 11
xp(5)=-x(2)*w(1+((12*x(2)*x(2))/(m*w*w)))*x(4);
Error in ==> odearguments at 98
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 172
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

 채택된 답변

Star Strider
Star Strider 2014년 9월 6일
편집: Star Strider 2014년 9월 6일

0 개 추천

I can’t run your code because at the [t,x] = ode45(... line, I get:
Undefined function or variable "x".
that probably relates to referring to it in your initial conditions.
Otherwise, you likely need to ‘vectorize’ your code, using the dot operator, replacing * with .*, / with ./, and ^ with .^.

댓글 수: 4

When i vectorize my code i just get the error
??? Undefined function or method 'x' for input arguments of type 'double'.
How can i refer to functions in my initial conditions if that is the problem? i tried to vectorize my initial conditions too but that did not fix the problem
I got it sorted. You have to declare the first two elements of your initial conditions first, so the last three can refer to them as necessary.
The full code, vectorised, with plot:
function xp=F(t,x)
xp=zeros(5,1);
m=1;
w=1;
h=.5;
y=.01;
xp(1)=-m.*(w.^2).*(x(2).^3).*(-(h.*y)./(2.*w.*m)).*24.*x(3);
xp(2)=x(1);
xp(3)=2.*w.*x(4);
xp(4)=-x(2).*w.*(1+((12.*x(2).*x(2))./(m.*w.*w))).*x(3)+w.*x(5);
xp(5)=-x(2).*w*(1+((12.*x(2).*x(2))./(m.*w.*w))).*x(4);
end
m=1;
w=1;
h=.5;
y=.01;
x0(1:2) = [0 1];
x0(3:5) = [.5*((1+((12*x0(2)*x0(2))/(m*w*w)))^(-.5)), 0, .5*((1+((12*x0(2)*x0(2))/(m*w*w)))^(.5))];
% x0 = x0';
[t,x]=ode45(@F,[0,10], x0);
figure(1)
plot(t, x)
grid
lgdstr = cellstr(strsplit(sprintf('q_{%d} ',1:5)));
legend(lgdstr(1:5));
You don’t need to vectorise you initial condition vector because all of its components are scalars.
Stephen
Stephen 2014년 9월 6일
Thank you very much
Star Strider
Star Strider 2014년 9월 6일
My pleasure!

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

추가 답변 (0개)

카테고리

질문:

2014년 9월 6일

댓글:

2014년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by