proble with differential equation

1 回表示 (過去 30 日間)
kris Holm
kris Holm 2021 年 1 月 2 日
編集済み: John D'Errico 2021 年 1 月 2 日
I am trying to run this code, but it says something is wrong. It's a differential equation y''+3y'=12x-5 it has to be answerd symbolic with dsolve.
I wrote like this, %1 works but %2 does not:
%1 this one works
syms f(x)
Df= diff(f,x);
dsolve(diff(diff(f))+3*diff(f)==12*x-5)
%2 this does not work
%Solve the initial value problem y=y''+3y'=12x-5 where y (0) = 1 and y'(0)=0 symbolic with dsolve. Call the reading f (x).
syms f(x)
Df= diff(f,x);
dsolve(diff(diff(f))+3*diff(f)==12*x-5), f(0) == 1, Df(0)== 0

回答 (1 件)

John D'Errico
John D'Errico 2021 年 1 月 2 日
編集済み: John D'Errico 2021 年 1 月 2 日
What did you do wrong? You wrote this:
dsolve(diff(diff(f))+3*diff(f)==12*x-5), f(0) == 1, Df(0)== 0
And how is that different from what I wrote below? You did not put the initial conditions INSIDE the call to dsolve. You had them on the same line as the call to dsolve, but that does not count! So close, really...
This works nicely:
syms y(x) % this defines y as an unknown function of the independent variable x.
Dy = diff(y,x,1); % first derivative, useful when we set the initial conditions
DDy = diff(y,x,2); % No real need to create this separately, but easier to read
ysol = dsolve(DDy + 3*Dy == 12*x - 5, y(0) == 1,Dy(0) == 0)
ysol =
2*x^2 - exp(-3*x) - 3*x + 2
Do you see the difference? The initial conditions need to be passed into dsolve for it to work properly.

カテゴリ

Help Center および File ExchangeEquation Solving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by