Determine where lines intersect
5 views (last 30 days)
Show older comments
Hey! Im wondering how i could Determinate the intersect of these two lines. I got the following code.
z0=10000;
dz0=0;
span=[0 250];
[T,Y] = ode45(@function_A,span,[z0 dz0]);
x=linspace(0,300,2000);
plot(T,Y(:,1),'b', x,0,'g');
and this function
function dz = function_A(t,z)
dz = zeros(2,1);
g=9.81;
m=250;
lambda=7.5*10^3;
k0=(m*g)/((250/3.6)^2);
k=@(z)k0.*exp(-z./lambda);
dz(1)=z(2);
dz(2)=-g-(k(z(1)).*abs(z(2))*z(2))/m;
end
Anyone who can help me?
0 Comments
Answers (1)
Mischa Kim
on 29 Sep 2014
Edited: Mischa Kim
on 29 Sep 2014
Hello Andreas, use ode events. This examples shows how to detect events with the bouncing ball problem. Check out the following:
function my_ode()
z0 = 10000;
dz0 = 0;
span=[0 250];
options = odeset('Events',@events);
[T,Y,Te,Ye,Ie] = ode45(@function_A,span,[z0 dz0],options);
x = linspace(0,300,2000);
plot(T,Y(:,1),'b', x,0,'g');
end
function dz = function_A(t,z)
dz = zeros(2,1);
g = 9.81;
m = 250;
lambda = 7.5*10^3;
k0 = (m*g)/((250/3.6)^2);
k = @(z)k0.*exp(-z./lambda);
dz(1) = z(2);
dz(2) = -g-(k(z(1)).*abs(z(2))*z(2))/m;
end
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a
% decreasing direction and stop integration.
value = y(1); % Detect height = 0
isterminal = 1; % Stop the integration
direction = 0; % Negative direction only
end
1 Comment
Jan
on 5 Oct 2014
The function to be integrated contains an abs() . This is a non-smooth function and inconsequence the solution suffers from the problems described here. The step size control of ODE45 must fail at the changes of the sign. To handle this, the step size is reduced until the discontinuity is covered by the rounding errors.
Better use an event function and restart the integration at the discontinuity.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!