필터 지우기
필터 지우기

How do I graph an ordinary differential equation with an initial condition?

조회 수: 2 (최근 30일)
I'm trying to solve a problem for a matlab problem set homework, and I was having issues getting the code below to work. The question in the book is "Plot the solution satisfying the initial condition y(2) = 1.5". Thank you!
syms y(t)
cond = y(2)== 1.5; %describes initial condition
ode = exp(y) + (( t*exp(y)-sin(y))* diff(y, t)) == 0; % the equation
ySol = dsolve(ode, cond)
figure
ezplot(ySol)

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 9월 15일
편집: Ameer Hamza 2020년 9월 15일
dsolve() shows that the ODE might not have a symbolic solution. So you will need to resort to a numerical solution. Following shows how it can be done in MATLAB
syms y(t)
ode = exp(y) + (( t*exp(y)-sin(y))* diff(y, t)) == 0; % the equation
V = odeToVectorField(ode);
odefun = matlabFunction(V, 'vars', {'t', 'Y'});
IC = 1.5;
tspan = [2 10]; % interval in which solution is needed
[t, y] = ode45(odefun, tspan, IC);
f = figure();
ax = axes();
plot(t, y);
This graph follows the initial condition.
  댓글 수: 2
Ramadan M
Ramadan M 2020년 9월 15일
Thank you! I understand this a lot more now!
Also, would you happen to know how to find y(some specified t) with code, and mark it on the graph? It's the follow up question to this one.
ex. y(1), mark this point on the graph.
I made a few attempts at it, but the textbook that I'm working out of seems to be fairly outdated.
Ameer Hamza
Ameer Hamza 2020년 9월 16일
See the following code
syms y(t)
ode = exp(y) + (( t*exp(y)-sin(y))* diff(y, t)) == 0; % the equation
V = odeToVectorField(ode);
odefun = matlabFunction(V, 'vars', {'t', 'Y'});
IC = 1.5;
tspan = [2 10]; % interval in which solution is needed
sol = ode45(odefun, tspan, IC);
f = figure();
ax = axes();
hold(ax);
plot(sol.x, sol.y);
plot(5, deval(sol, 5), '*', 'MarkerSize', 8, 'LineWidth', 2);

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by