Programming error with a model function

2 Ansichten (letzte 30 Tage)
Chatowa
Chatowa am 31 Jul. 2023
Bearbeitet: Torsten am 31 Jul. 2023
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);
Unrecognized function or variable 'dyB'.

Error in solution>fun40 (line 11)
dyA(1) = dyB(2);

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode15s (line 153)
odearguments(odeIsFuncHandle, odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
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

Akzeptierte Antwort

Torsten
Torsten am 31 Jul. 2023
Bearbeitet: Torsten am 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)
eqn(t) = 
y1sol = dsolve(eqn)
y1sol = 
y2sol = A/B * t * y1sol
y2sol = 
diff(y1sol,t)
ans = 
simplify(diff(y2sol,t))
ans = 

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 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 Kommentar
Walter Roberson
Walter Roberson am 31 Jul. 2023
syms A B positive
syms y1(t) y2(t)
dy1 = diff(y1);
dy2 = diff(y2);
eqn1 = dy1 == dy2
eqn1(t) = 
eqn2 = y2 == A/B * t * y1
eqn2(t) = 
sol = dsolve([eqn1, eqn2])
Warning: Unable to find symbolic solution.
sol = [ empty sym ]
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
y1(t) = 
eqn3 = y2 == A/B * t * y1
eqn3(t) = 
sol = isolate(eqn3, y2)
sol = 
y2 = rhs(sol)
y2 = 
y1 = simplify(y2 + C)
y1 = 
simplify(diff(y1) - diff(y2))
ans = 
0

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by