Undefined Variable Error in Function
Show older comments
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
Walter Roberson
on 25 Feb 2020
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.
Sarah Peterson
on 25 Feb 2020
Pravin Jagtap
on 28 Feb 2020
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.
Answers (0)
Categories
Find more on Numerical Integration and 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!