Undefined Variable Error in Function

For my homework we have to plot a second order differential equation. I believe I have the basic structure of how to do this down but I keep getting and error about one of my variables.
I keep getting an error saying that x is not defined, however I can't seem to find a place where I would define it as the second order equation is acceleration and in terms of velocity. Is there a place where I can define x without messing up the rest of my code or a way to get MATLAB to ignore it.
v0 = 10; %initial velocity
theta = 45; % degrees
m = 1; %kg
x0 = 0; %Initial x
y0 = 0; % Initial y
g = 9.8; %gravity
tspan = [0 ; .0001 ; .3]; %seconds
[t,x] = ode45(@f,tspan,x0);
function fix = f(t,x)
x = X(1);
v = X(2);
c = 0;
fix = [ v ; -c*v*cos(theta)/1*(sqrt(v*cos(theta)+v*sin(theta)))^(p-1) ];
end

3 Comments

What is y0? Why do you assign to it but not use it?
You pass x0 as the only initialization for the boundary conditions. As your x0 is a scalar you are informing matlab that you have a system with one state. But your code attempts to access two states.
The homework has multiple parts and includes a y-component section as well, hence the y0 and gravity. That area is just so I can keep track of all of my initial conditions. When I integrated the equations by hand the intial x was to be added onto the final x position equation. However x never appears as a variable in any of the equations I know the final equation is x=, but I'm not sure how to reach it using MATLAB
Hello Sarah,
Can you let us know which equation you want to solve? (Also specify the initial and boundary conditions) You mentioned the solution of the governing equation. I can see that you are dealing with projectile motion.

Sign in to comment.

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 25 Feb 2020

Commented:

on 28 Feb 2020

Community Treasure Hunt

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

Start Hunting!