function dxdt = pendulum(t,x)
L = 1;
theta = x(1);
gamma = x(2);
dtheta = gamma;
dgamma = -(9.8/L)*sin(theta);
dxdt = zeros(2,1);
dxdt(1)=dtheta;
dxdt(2)=dgamma;
[t,x]=ode45('pendulum',[0 10],[0.9*pi 0]);
plot(t,x(:,1));
hold on;
plot(t,x(:,2),'r');
legend('Position','Velocity');
plot(x(:,1),x(:,2));
xlabel('Position');
yLabel('Velocity');
It says x is undefined

댓글 수: 2

dpb
dpb 2014년 5월 18일
Need EXACT error in context including traceback to be able to tell--too many places where x is used. You may well need to change the internal use of x as the result of ode45 and where it's the passed-in value; didn't try to read the code thoroughly enough to tell for certain.
Image Analyst
Image Analyst 2014년 5월 18일
Well one problem is that the formatting is bad. Read this to correct it: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup

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

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 5월 18일
편집: Azzi Abdelmalek 2014년 5월 18일

0 개 추천

There is no error in your code, the problem is the way you are using it.
Save the followed code as pendulum.m
function dxdt = pendulum(t,x)
L = 1;
theta = x(1);
gamma = x(2);
dtheta = gamma;
dgamma = -(9.8/L)*sin(theta);
dxdt = zeros(2,1);
dxdt(1)=dtheta;
dxdt(2)=dgamma;
Then in another file write and run this code
[t,x]=ode45('pendulum',[0 10],[0.9*pi 0]);
plot(t,x(:,1));
hold on;
plot(t,x(:,2),'r');
legend('Position','Velocity');
plot(x(:,1),x(:,2));
xlabel('Position');
ylabel('Velocity');

댓글 수: 3

Andrew Davies
Andrew Davies 2014년 5월 18일
편집: Andrew Davies 2014년 5월 18일
Something is going wrong. Here is another code i'm actually trying to get to work
%function dLdt = Lorentz(t,x,y,z)
%s = 10
%b = 8/3
%x = L(1);
%y = L(2);
%z = L(3);
%dx = s*(y - x)
%dy = r*x - y - x*z
%dz = x*y - b*z
%dLdt = zeros(3,2,1);
%dLdt(1)=dx;
%dLdt(2)=dy;
%dLdt(3)=dz;
That is the one .m file i made. The second one is simply
%[t,x,y,z]=ode45('pro1',0:0.001:1,[1 1 1]);
%plot3(L(:,1),L(:,2),L(:,3));
Now it says 'Undefined function or method 'L' for input arguments of type 'double' pro1 is the .m file
Azzi Abdelmalek
Azzi Abdelmalek 2014년 5월 18일
Andrew, what about your original question?
Andrew Davies
Andrew Davies 2014년 5월 18일
That was an example from a textbook and i was using it to try to understand how to solve my own one, the second one i described

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

추가 답변 (0개)

카테고리

질문:

2014년 5월 18일

댓글:

2014년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by