Ode45 g and dg

2 views (last 30 days)
Elahe S
Elahe S on 13 Oct 2024
Commented: Walter Roberson on 13 Oct 2024
For solving the equation dg2, dg6 is needed. Therefore the following error is shown:
Undefined function or variable 'dg4'.
How can it be fixed?
options = odeset('RelTol',1e-5,'AbsTol',1e-5);
[t,g] = ode45(@2system(t,g,t1,A, m, k)
function dg=2system(t,g,t1,A, m, k)
A1 = interp1(t1,A1x,t); % Interpolate the data set (t1,A1x) at time t
dg1=g(2);
dg2=m*A+k*g(1)+h1*k*g(3)+dg4
dg3=g(4);
dg4=h1*k2x*g(1)+dg2
dg=[dg1;dg2;dg3;dg4];
end
  1 Comment
Walter Roberson
Walter Roberson on 13 Oct 2024
[t,g] = ode45(@2system(t,g,t1,A, m, k)
is invalid syntax.
function dg=2system(t,g,t1,A, m, k)
That is an invalid function name. Function names must begin with an upper or lower case Latin letter.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 13 Oct 2024
Edited: John D'Errico on 13 Oct 2024
You cannot use the variable dg4, before it is defined. And we see you do this:
dg2=m*A+k*g(1)+h1*k*g(3)+dg4
...
dg4=h1*k2x*g(1)+dg2
You use dg4 to define dg2, but dg4 does not exist at that time. And in fact, you then use dg2 to define the value of dg4 later on.
It might seem this just forces us to solve the linear system of equatinos, for the two unknowns dg2 and dg4. Well, CAN you solve the 2x2 linear system of equations to solve for those two values? Well, it might seem to be the case, but in fact, that linear system of equations is singular.
That singularity tells me that unless a very specific scenario exists, no solution is ever possible. Why not? Add the two equations together.
dg2=m*A+k*g(1)+h1*k*g(3)+dg4
plus...
dg4=h1*k2x*g(1)+dg2
Which yields:
dg2 + dg4 = m*A+k*g(1)+h1*k*g(3) + dg4 + h1*k2x*g(1) + dg2
Note that we have dg2 + dg4 on both sides of the equation. Drop them. Toss them in the bit bucket of mathematics. What remains tells us that:
m*A + k*g(1) + h1*k*g(3) + h1*k2x*g(1) == 0
So unless that expression is identically equal to zero, then NO solution exsts for dg2 and dg4.
And IF it is true that the above expression does equal zero, IDENTICALLY, then we have the scenario where you can choose any completely arbitrary value for dg4, and dg2 will be exactly dg4.
I'm sorry, but that is how mathematics works. In any case, your equations do not result in anything useful. They cannot do so, because either dg2 and dg4 do not exist, or they are completely arbitrary. Since neither case is acceptable, you need to return and look far more carefully at your equations.

Tags

Community Treasure Hunt

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

Start Hunting!