Programming error with a model function

조회 수: 1 (최근 30일)
Chatowa
Chatowa 2023년 7월 31일
편집: Torsten 2023년 7월 31일
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

채택된 답변

Torsten
Torsten 2023년 7월 31일
편집: Torsten 2023년 7월 31일
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 = 

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 7월 31일
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
Walter Roberson
Walter Roberson 2023년 7월 31일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by