Solving differential equation using matlab

17 ビュー (過去 30 日間)
Bob Gill
Bob Gill 2023 年 4 月 2 日
編集済み: Dyuman Joshi 2023 年 4 月 2 日
Hey everyone.
I just need some help solving this differential equation with matlab, and then plotting it.
This is my current code, but it does not seem to work.
y = dsolve('D2x-2Dx+5x=dirac(t-5)', 'x(0)=0, Dx(0)=0, t=0', 't')
ezplot(y)
  1 件のコメント
Sam Chak
Sam Chak 2023 年 4 月 2 日
編集済み: Sam Chak 2023 年 4 月 2 日
Bob, it is invalid to name variables that begin with digits, like "2Dx" and "5x". For details, see:
https://www.mathworks.com/help/matlab/matlab_prog/variable-names.html
Instead of pasting the full solution where you can directly copy, it will be more meaningful to find out which example in dsolve that you refer.
Knowing that will help improving the way of technical writing in dsolve documentation that beginners can search for relevant examples, follow the mechanics and understand the code easily.

サインインしてコメントする。

回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2023 年 4 月 2 日
Do step-by-step and avoid using strings, like this:
syms t real
syms x(t)
Dx = diff(x);
D2x = diff(x,2);
y = dsolve(D2x - 2*Dx + 5*x == dirac(t-5),x(0)==0, Dx(0)==0);
ezplot(y)
HTH

Dyuman Joshi
Dyuman Joshi 2023 年 4 月 2 日
編集済み: Dyuman Joshi 2023 年 4 月 2 日
Hello Bob,
It is recommended to use symbolic variables instead of using string or char arrays in dsolve (as you can see in the documentation) and it is recommended to use fplot over ezplot
%single variable function
syms x(t)
Dx = diff(x);
%differential eequation6
eq = diff(x,2) - 2*diff(x) + 5*x == dirac(t-5)
eq(t) = 
%Initial conditions
con = [x(0) == 0; Dx(0) == 0];
sol = dsolve(eq,con)
sol = 
%Adjust the interval as necessary
fplot(sol,[0 10])

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by