Nique
Nique

Reputation: 299

Finding x intercept in MATLAB

I am trying to find the x intercept of a parabola of a plot using

x0 = interp1(y,x,0)

However because my parabola starts at the origin it returns 0.

How do I find the x intercept that lies away from the origin? At the moment I am estimating by eye-ball.

Code for the plot:

global k g

g = 10;
v0 = 150;
theta = pi/4;
m = 1;
k = 0.001;

tspan = [0 22];

IC = [0; v0*cos(theta); 0; v0*sin(theta)];
[t, oput] = ode45(@dataODE, tspan, IC);

x = oput(:,1);
vx = oput(:,2);
y = oput(:,3);
vy = oput(:,4);

figure(1); clf;
plot(x,y)

where

function [p] = dataODE( t, x)

global k g

p = zeros(4,1);
p(1) = x(2);
p(2) = -k*sqrt(x(2)^2 + x(4)^2)* x(2);
p(3) = x(4);
p(4) = -g -k*sqrt(x(2)^2 + x(4)^2)* x(4);

Upvotes: 3

Views: 1449

Answers (1)

Dan
Dan

Reputation: 45752

You could just restrict x and y to only contain the one intercept:

x0 = interp1(y(a:b),x(a:b),0)

but how do you find a and b? One way would be to just use the points before and after y crosses zero (on the way down):

a = find(diff(y > 0) == -1)
b = a+1

Upvotes: 1

Related Questions