Programming error with a model function
4 views (last 30 days)
Show older comments
I don't understand why but I'm getting an error message regarding my Matlab script:
I'm trying to code for the example equation
y′1=y′2
y2=A/Bty1.
tspan = [0 5];
y0 = [0 0.1];
A = 1;
B = 2;
[t,yAB] = ode15s(@fun40,tspan,y0);
figure
semilog(t,y)
grid
function dyAB = fun40(t,yAB)
dyAB = zeros(2,1);
dyA(1) = dyB(2);
dyB(2) = (A/B)*t.*dyA(1);
end
0 Comments
Accepted Answer
Torsten
on 31 Jul 2023
Edited: Torsten
on 31 Jul 2023
If your system of differential equations really reads
y1' = y2'
y2 = A/B * t * y1
it follows
y2' = A/B * y1 + A/B * t * y1'
thus
y1' = A/B * y1 / (1- A/B * t)
syms t y1(t)
syms A B real
eqn = diff(y1,t) == A/B * y1 / (1- A/B * t)
y1sol = dsolve(eqn)
y2sol = A/B * t * y1sol
diff(y1sol,t)
simplify(diff(y2sol,t))
0 Comments
More Answers (1)
Walter Roberson
on 31 Jul 2023
tspan = [0 5];
y0 = [0 0.1];
A = 1;
B = 2;
[t,yAB] = ode15s(@(t,y)fun40(t,y,A,B),tspan,y0);
figure
plot(t,yAB)
grid
ylim([-0.01 0.11])
function dyAB = fun40(t,yAB,A,B)
dyAB = zeros(2,1);
dyA(1) = yAB(2);
dyB(2) = (A/B)*t.*yAB(1);
end
1 Comment
Walter Roberson
on 31 Jul 2023
syms A B positive
syms y1(t) y2(t)
dy1 = diff(y1);
dy2 = diff(y2);
eqn1 = dy1 == dy2
eqn2 = y2 == A/B * t * y1
sol = dsolve([eqn1, eqn2])
However, if the derivatives of the two functions are equal then the two functions differ only by a constant, and so you can substitute into
syms C
y1 = y2 + C
eqn3 = y2 == A/B * t * y1
sol = isolate(eqn3, y2)
y2 = rhs(sol)
y1 = simplify(y2 + C)
simplify(diff(y1) - diff(y2))
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

