Finding x intercept in MATLAB - 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);

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

Related

Plot two-dimensional array as elliptical orbit

I am attempting to plot an elliptical orbit based on a 2-D position array, beginning at p= [5 0]. The plot charts positions from a timeframe t, here between 0 and 100. The calculation uses the formula F = -(p/||p||)(m*M/(p^2) to approximate acceleration and velocity. The result should look like the following:
This is my current code. It plots a completely different shape. Is the problem in my way of interpreting the force equation?
Any other help and comments are much appreciated.
t = 0; p = [50 0]; v = [0 8]; %Initial conditions
dt = 0.05;
M = 10000; m= 1;
tmax = 100;
figure, hold on
d = 0.001
clf;
while t < tmax
F = -(p./norm(p)).*(m*M./(p*p'));
a = F./m - d*v;
v = v + a*dt;
p = p + v*dt;
t = t + dt;
plot(p(1),t,'o','MarkerSize',0.5);
hold on;
plot(p(2),t,'o','MarkerSize',0.5);
end
You want p(1) in the x axis and p(2) in the y axis, with t as a parameter. So you need to replace the two plot lines by plot(p(1),p(2),'o','MarkerSize',0.5); (keeping hold on):
t = 0; p = [50 0]; v = [0 8]; %Initial conditions
dt = 0.05;
M = 10000; m= 1;
tmax = 100;
figure, hold on
d = 0.001
clf;
while t < tmax
F = -(p./norm(p)).*(m*M./(p*p'));
a = F./m - d*v;
v = v + a*dt;
p = p + v*dt;
t = t + dt;
hold on
plot(p(1),p(2),'o','MarkerSize',0.5); %%% modified line
end

MATLAB plotting trajectory

I would like to plot the angular velocity of point B by using MATLAB. However, there is some mistake in my code for angular velocity that I couldn't fix.
The length of the input link OA of the mechanism is r = 50 mm, length of AB is l = 150 mm. Fixed coordinates of point C are xC = d = 80 mm and yC = 0 mm.
Angular velocity of OA is ω = 15 rad/s.
%Full trajectory of B
%Linkage dimensions
clear
r = 50;
l = 150;
xC = 80;
yC = 0;
n = 361; % Number of position calculations
fii = linspace(0,2*pi,n);
omega = 15;
[xA,yA] = pol2cart(fii,r);
d = xA + xC;
alpha = atan(yA./(xC-xA));
lx = l*cos(alpha);
ly = l*sin(alpha);
xB = xA + lx;
yB = yA + ly;
plot(xB,yB) %Plots the trajectory
title('Full trajectory of AB')
% Angular velocity of AB
for ind = 1:n
omegaAB(ind) = (-(r^2-d*r*cos(fii))/(r^2 + d^2 - 2*d*r*cos(fii)))*omega;
end
figure(2)
plot(fii,omegaAB, 'linewidth', 2, 'color', 'red')
title('Angular velocity of AB')
ylabel('\omega_{AB} [1/s]')
xlabel('\phi [rad]')
I think there is just one mistake in your code when you compute alpha:
alpha = atan(yA./(xC-xA));
This gives the following trajectory:
Your code is partially vectorized but you're still looping over ind which is why you are getting matrix dimensions errors. You can either remove the loop and make it completely vectorized or you can make sure that all of your vectors with length n are indexed properly:
for ind = 1:n
omegaAB(ind) = (-(r^2-d*r*cos(fii(ind)))/(r^2 + d(ind)^2 - 2*d*r*cos(fii(ind))))*omega;
end

Modelling Projectile Motion using Matlab ode45

So I am trying to model simple projectile motion (no air resistance etc) using the ode45 solver in Matlab. This is my code so far:
function [x,y] = trajectory_without_AR_45(v0,theta, dt)
%Path of mortar without air resistance using ode45
g = 9.81;
t_start = 0;
t_end = 100;
%Initial Conditions
y01 = 0; %initial x
y02 = v0 * cos(theta); %finding initial velocity in x direction
y03 = 0; %initial y
y04 = v0 * sin(theta); % finding intial velocity in y direction
y0 = [y01;y02;y03;y04];
%Derivatives
dy1 = y0(2); %vx
dy2 = 0; %ax
dy3 = y0(4); %vy
dy4 = -g; %ay
dy = [dy1;dy2;dy3;dy4];
%Using ODE45
f = # (t, y) (dy);
solution = ode45(f, [t_start, t_end], y0);
t = t_start : 0.01: t_end;
y = deval(solution, t);
plot (y(:,1), y(:,3)); %plotting trajectory
end
However, the plot I am getting is just a straight line which clearly is not correct. Any help would be appreciated.
I think you're not calling the ode solver properly, you only seem to be using the initial conditions to compute dy, which is wrong. Best to put the ode function in a separate function (file). For example, the following is in missile.m:
function dY = missile(t,Y)
g = 9.81;
dY(1) = Y(2); %vx
dY(2) = 0; %ax
dY(3) = Y(4); %vy
dY(4) = -g; %ay
end
Which you would then call as follows:
t_start = 0;
t_end = 2.3; % changed to something more consistent with when y<0
v0 = 10; % made up
theta=pi/4; % made up
y01 = 0; %initial x
y02 = v0 * cos(theta); %finding initial velocity in x direction
y03 = 0; %initial y
y04 = v0 * sin(theta); % finding intial velocity in y direction
y0 = [y01;y02;y03;y04];
opts = odeset('MaxStep',0.01,'InitialStep',0.001);
[t,y] = ode45(#missile, [t_start, t_end], y0, opts);
plot(t,y)
legend('x','dx','y','dy')
which gives the following results:

Plot solution of second order equation in MATLAB

Could you please help me with the following question:
I want to solve a second order equation with two unknowns and use the results to plot an ellipse.
Here is my function:
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c
V is 2x2 symmetric matrix, c is a positive constant and there are two unknowns, x1 and x2.
If I solve the equation using fsolve, I notice that the solution is very sensitive to the initial values
fsolve(fun, [1 1])
Is it possible to get the solution to this equation without providing an exact starting value, but rather a range? For example, I would like to see the possible combinations for x1, x2 \in (-4,4)
Using ezplot I obtain the desired graphical output, but not the solution of the equation.
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
ezplot(fh)
axis equal
Is there a way to have both?
Thanks a lot!
you can take the XData and YData from ezplot:
c = rand;
V = rand(2);
V = V + V';
fh= #(x1,x2) [x1 x2]*V*[x1 x2]'-c;
h = ezplot(fh,[-4,4,-4,4]); % plot in range
axis equal
fun = #(x) [x(1) x(2)]*V*[x(1) x(2)]'-c;
X = fsolve(fun, [1 1]); % specific solution
hold on;
plot(x(1),x(2),'or');
% possible solutions in range
x1 = h.XData;
x2 = h.YData;
or you can use vector input to fsolve:
c = rand;
V = rand(2);
V = V + V';
x1 = linspace(-4,4,100)';
fun2 = #(x2) sum(([x1 x2]*V).*[x1 x2],2)-c;
x2 = fsolve(fun2, ones(size(x1)));
% remove invalid values
tol = 1e-2;
x2(abs(fun2(x2)) > tol) = nan;
plot(x1,x2,'.b')
However, the easiest and most straight forward approach is to rearrange the ellipse matrix form in a quadratic equation form:
k = rand;
V = rand(2);
V = V + V';
a = V(1,1);
b = V(1,2);
c = V(2,2);
% rearange terms in the form of quadratic equation:
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2) = k;
% a*x1^2 + (2*b*x2)*x1 + (c*x2^2 - k) = 0;
x2 = linspace(-4,4,1000);
A = a;
B = (2*b*x2);
C = (c*x2.^2 - k);
% solve regular quadratic equation
dicriminant = B.^2 - 4*A.*C;
x1_1 = (-B - sqrt(dicriminant))./(2*A);
x1_2 = (-B + sqrt(dicriminant))./(2*A);
x1_1(dicriminant < 0) = nan;
x1_2(dicriminant < 0) = nan;
% plot
plot(x1_1,x2,'.b')
hold on
plot(x1_2,x2,'.g')
hold off

How to plot pendulum motion for Elastic Pendulum with Spring in Matlab

I have a code that creates the correct xy plot for elastic pendulum with spring. I would like to show an animation of the elastic spring pendulum on an xy plot as the system marches forward in time. How can this be done?
Here is my simulation code:
clc
clear all;
%Define parameters
global M K L g;
M = 1;
K = 25.6;
L = 1;
g = 9.8;
% define initial values for theta, thetad, del, deld
theta_0 = 0;
thetad_0 = .5;
del_0 = 1;
deld_0 = 0;
initialValues = [theta_0, thetad_0, del_0, deld_0];
% Set a timespan
t_initial = 0;
t_final = 36;
dt = .1;
N = (t_final - t_initial)/dt;
timeSpan = linspace(t_final, t_initial, N);
% Run ode45 to get z (theta, thetad, del, deld)
[t, z] = ode45(#OdeFunHndlSpngPdlmSym, timeSpan, initialValues);
% initialize empty column vectors for theta, thetad, del, deld
M_loop = zeros(N, 1);
L_loop = zeros(N, 1);
theta = zeros(N, 1);
thetad = zeros(N, 1);
del = zeros(N, 1);
deld = zeros(N, 1);
T = zeros(N, 1);
x = zeros(N, 1);
y = zeros(N, 1);
% Assign values for variables (theta, thetad, del, deld)
for i = 1:N
M_loop(i) = M;
L_loop(i) = L;
theta(i) = z(i, 1);
thetad(i) = z(i, 2);
del(i) = z(i, 3);
deld(i) = z(i, 4);
T(i) = (M*(thetad(i)^2*(L + del(i))^2 + deld(i)^2))/2;
V(i) = (K*del(i)^2)/2 + M*g*(L - cos(theta(i))*(L + del(i)));
E(i) = T(i) + V(i);
x(i) = (L + del(i))*sin(theta(i));
y(i) = -(L + del(i))*cos(theta(i));
end
figure(1)
plot(x, y,'r');
title('XY Plot');
xlabel('x position');
ylabel('y position');
Here is my function code:
function dz = OdeFunHndlSpngPdlmSym(~, z)
% Define Global Parameters
global M K L g
% Take output from SymDevFElSpringPdlm.m file for fy1 and fy2 and
% substitute into z2 and z4 respectively
%fy1=thetadd=z(2)= -(M*g*sin(z1)*(L + z3) + M*z2*z4*(2*L + 2*z3))/(M*(L + z3)^2)
%fy2=deldd=z(4)=((M*(2*L + 2*z3)*z2^2)/2 - K*z3 + M*g*cos(z1))/M
% return column vector [thetad; thetadd; deld; deldd]
dz = [z(2);
-(M*g*sin(z(1))*(L + z(3)) + M*z(2)*z(4)*(2*L + 2*z(3)))/(M*(L + z(3))^2);
z(4);
((M*(2*L + 2*z(3))*z(2)^2)/2 - K*z(3) + M*g*cos(z(1)))/M];
You can "simulate" animation in a plot with a continous updating FOR loop and assignig graphic object to variables.
Something like (I assume only to use x,y as arrays function of time array t)
%In order to block the axis and preventing continuous zoom, choose proper axes limit
x_lim = 100; %these values depends on you, these are examples
y_lim = 100;
axis equal
axis([-x_lim x_lim -y_lim y_lim]); %now x and y axes are fixed from -100 to 100
ax = gca;
for i=1:length(t)
if i > 1
delete(P);
delete(L);
end
P = plot(x(i),y(i)); %draw the point
hold on
L = quiver(0,0,x(i),y(i)); %draw a vector from 0,0 to the point
hold on
%other drawings
drawnow
pause(0.1) %pause in seconds needed to simulate animaton.
end
"Hold on" instruction after every plot instruction.
This is only a basic animation, of course.