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 件のコメント

Walter Roberson
Walter Roberson 2020 年 2 月 25 日
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
Sarah Peterson 2020 年 2 月 25 日
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
Pravin Jagtap
Pravin Jagtap 2020 年 2 月 28 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

リリース

R2018a

質問済み:

2020 年 2 月 25 日

コメント済み:

2020 年 2 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by