Evaluating an ODE until you reached a certain distance using a while loop: Train/distance problem

Hello, I was wondering how you'd evaluate a ode until it reaches a wanted distance. For me I'm trying to code it so that it run until R is equal to the distance, which is 3. Any tips on were I'm going wrong? Thanks
% code
function tr()
gr = 4.66; r = 1; D = 2;
options = odeset('events', @location);
[t,R] = ode45(@coupled,[0,3],[r;3],3);
V=R(1,2);
plot(t,R(:,1),'o',t,R(:,2),'o');
shg
%end
[T1,Y1,te,ye,ie]=ode45(@coupled,[1:1:50], [1;3],options);
disp(te);
function [lookfor,stop,direction]=location(t,y)
lookfor=y(2)-3; % Stop at 3
stop=1;
direction=0;
end
function dYdt = coupled(w,Y)
dYdt = zeros(1,2);
dYdt = [Y(1);-(pi^2/6)^2*cos(pi*w)^2-gr*sin((pi/8)*cos(pi*w))];
end
end
end

5 件のコメント

It is not clear to me what you are trying to do. Do you want to:
1) Solve the ODE on the interval [0,3] and vary the initial value of t until R(x=3) = 3?
2) Solve for an interval [0,xmax] so that R(x=xmax) = 3?
Or something else entirely?
In case 2) have a look at the option 'events' in odeset .
panacean
panacean 2018 年 4 月 28 日
I apologize for not being clear. so I have a train on a set of tracks and I'm trying to calculate how long it will take for my train to get to a distance of 3 miles. where r is the current distance of the train and D is the goal of 3 miles.
I am still confused. Is x and R both distances? Could you post your coupled function and explain the meaning of the variables x, t and R?
panacean
panacean 2018 年 5 月 2 日
編集済み: panacean 2018 年 5 月 2 日
So added more of my code, with some changes, I'm trying to find how long it takes the train to go 3 miles. My ode with the options event isn't doing the search like I wanted it to do. Any tips?
I would still like a better explanation of what you are trying to do. If I interpret your function coupled(w,Y) correctly, you try to so solve the coupled differential equations
What is the independent variable w? Time? What are the two components of Y. (Is Y(1) = R?).
Note that your equations are not coupled at all, but two independent differential equations. Note also that the variable gr must be given a value.
The analytical solution is:
I am not sure that this is what you want. Please give a better description of the original problem.

サインインしてコメントする。

 採用された回答

Jan
Jan 2018 年 5 月 3 日
lookfor=y(1)+3
This stops at y(1) == -3, not +3. You want:
lookfor = y(1) - 3;

5 件のコメント

panacean
panacean 2018 年 5 月 3 日
Okay I changed it, still not working. Is this the right way to go about it? Trying to see how long it takes for r which starts at 1 takes for it to get to 3 miles. Changed my code above. Any tips would be great.
dYdt = (pi^2/18)^2*r*sin(pi*t).^2-gr*sin(pi/16.*cos(pi*t);
- "r" and "gr" are undefined
- a closing parenthesis is missing after "sin(pi/16"
- the solution variable "y" is not used in the calculation of dYdt.
panacean
panacean 2018 年 5 月 3 日
Sorry I posted some older code.
If you set
lookfor = y(2)-3;
you search for the time t where velocity (y') becomes 3. I think you mean
lookfor = y(1)-3;
Best wishes
Torsten.
Jan
Jan 2018 年 5 月 3 日
@panacean: How can we help you to find the error in the code, if all we see is an older version? "Still not working" does not allow to understand, what you observe. Without the code and information about what the code does, I cannot understand, what the problem is.

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2018 年 4 月 30 日
Use the "Event" option of ode45 to locate the t-value at which R=3.
Best wishes
Torsten.

1 件のコメント

panacean
panacean 2018 年 5 月 2 日
Added my code with the changes, still coming out right. Any tips? The Event option is definitely the right way to go though.

サインインしてコメントする。

カテゴリ

質問済み:

2018 年 4 月 28 日

コメント済み:

Jan
2018 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by