Solving 2nd Order Differential Equation Symbolically
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
I have the 2nd order differential equation: y'' + 2y' + y = 0 with the initial conditions y(-1) = 0, y'(0) = 0.
I need to solve this equation symbolically and graph the solution.
Here is what I have so far...
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0])
a = linspace(0,1,20);
b = eval(vectorize(ySol));
plot(a,b)
But I get the following output.
ySol = 

Error using eval
Unrecognized function or variable 'C1'.
I'd greatly appreciate any assistance.
採用された回答
Star Strider
2022 年 4 月 15 日
The
constants are the initial condition. They must be defined.
constants are the initial condition. They must be defined. syms y(x) y0
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol(x,y0) = dsolve(ode,[Dy(0)==0,y(-1)==0,y(0)==y0])
ySol(x, y0) = 

% a = linspace(0,1,20);
% b = eval(vectorize(ySol));
figure
fsurf(ySol,[0 1 -1 1])
xlabel('x')
ylabel('y_0 (Initial Condition)')

.
16 件のコメント
Jordan Stanley
2022 年 4 月 15 日
Thank you for the reply.
I believe this is what I needed.
Quick question, do you know if the solution could be graphed in simply 2-D.
It can, however that requires either a range of initial conditions, or to choose one.
ySol = @(x, y0) exp(-x) .* (y0 + x.*y0);
xv = linspace(0, 1, 20);
y0v = linspace(-5, 5, 11)
y0v = 1×11
-5 -4 -3 -2 -1 0 1 2 3 4 5
[X,Y0] = ndgrid(xv,y0v);
figure
plot(xv, ySol(X,Y0))
grid
xlabel('x')
ylabel('y_0')

The solutions all converge to zero at about x=10.
.
Jordan Stanley
2022 年 4 月 15 日
I see. Thank you for the response.
Star Strider
2022 年 4 月 15 日
As always, my pleasure!
Hello again,
Upon running your initially suggested code
syms y(x) y0
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol(x,y0) = dsolve(ode,[Dy(0)==0,y(-1)==0,y(0)==y0])
% a = linspace(0,1,20);
% b = eval(vectorize(ySol));
figure
fsurf(ySol,[0 1 -1 1])
xlabel('x')
ylabel('y_0 (Initial Condition)')
I get the following error message.
Error using sym/subsindex
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
I have the following code in the section above. I'm not sure if this causes the issue.
% Finds solution to the DE
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0])
dySol = diff(ySol,x);
ySol = @(x) exp(-x).*(1+x);
dySol = @(x) -exp(-x).*x;
% Sets up directional field
[x,y]=meshgrid(-4:0.5:4,-4:0.5:4);
u = y; % x1' = y'
v = - 2*y - x; % x2' = y'' = - 2*y' - y
u1 = u./sqrt(u.^2+v.^2);
v1 = v./sqrt(u.^2+v.^2);
quiver(x, y, u1, v1, 0.6)
xlabel('x-axis')
ylabel('y-axis')
axis on
axis([-4 4 -4 4]);
title('Direction Field')
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4),"LineWidth",2);
hold off
Thanks
Works for me online here.
% Finds solution to the DE
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0])
ySol = 

dySol = diff(ySol,x);
ySol = @(x) exp(-x).*(1+x);
dySol = @(x) -exp(-x).*x;
% Sets up directional field
[x,y]=meshgrid(-4:0.5:4,-4:0.5:4);
u = y; % x1' = y'
v = - 2*y - x; % x2' = y'' = - 2*y' - y
u1 = u./sqrt(u.^2+v.^2);
v1 = v./sqrt(u.^2+v.^2);
quiver(x, y, u1, v1, 0.6)
xlabel('x-axis')
ylabel('y-axis')
axis on
axis([-4 4 -4 4]);
title('Direction Field')
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4),"LineWidth",2);
hold off

Star Strider
2022 年 4 月 15 日
(I was away doing other things for a few minutes.)
It worked along with the code you suggested as well?
syms y(x) y0
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol(x,y0) = dsolve(ode,[Dy(0)==0,y(-1)==0,y(0)==y0])
% a = linspace(0,1,20);
% b = eval(vectorize(ySol));
figure
fsurf(ySol,[0 1 -1 1])
xlabel('x')
ylabel('y_0 (Initial Condition)')
Do you want to see more solution curves in the plot of the direction field apart from the red one ?
The red curve is the one with initial condition y(0) = 1.
Or why do you want to use the code from above ?
Jordan Stanley
2022 年 4 月 15 日
The code for that was suggested for me to use for the question I asked above gave me an error when I ran it and I was just asking if maybe the error has somthing to do with the direction field and solution curve code that I had in the section above it.
I don't believe I need anymore solution curves.
You are told to solve the equation
y'' + 2y' + y = 0
with the initial conditions
y(-1) = 0, y'(0) = 0
symbolically and graph the solution.
We found out that there is not only one such solution, but that there are infinitly many, namely
ySol = exp(-x)*(C1 + C1*x)
for an arbitrary value of C1.
Now we took one of these solutions (red curve in plot), namely exp(-x)*(1+x) (C1=1) and integrated it in the direction field.
If you want to graph more solution curves therein, you can choose another value for C1 and include the graph similarly in the plot.
Jordan Stanley
2022 年 4 月 16 日
I believe that I just need to graph the solution of the equation found symbolically but not necessilary on the directional field.
Torsten
2022 年 4 月 16 日
The title of your last query was "Plotting Solution Curve on Direction Field" ...
Jordan Stanley
2022 年 4 月 16 日
Correct.
One of the tasks that I am suppsed to complete is to...
- Plot the direction field (phase portrait) of solutions to the equation (without initial conditions) in MATLAB. Print the graph to locate and trace the solution curve corresponding to the stated initial conditions.
Which I believe I have already done. The second task is to...
2. Use MATLAB to solve the equation symbolically and graph the solution.
This is what I'm currently seeking assistance for.
How should it be possible to solve 1. without 2. ? If you don't know the solution, you can't trace a solution curve. Or what's your opinion ?
Anyhow - I think your instructors overlooked that the equation together with its initial conditions does not only give one curve, but infinitly many. So "graphing the solution" will become difficult. But Star Strider's answer for this situation looks fine for me.
But you say you get an error. What's your code and what's the error message ?
Jordan Stanley
2022 年 4 月 16 日
編集済み: Jordan Stanley
2022 年 4 月 16 日
Well I should mention that we were supposed to choose a 2nd order differential eqaution initial value problem from our textbook and maybe I just happened to choose a more complicated eqaution to use.
Ok, well interestingly enough upon running the code agin that Star Strider suggested I did not get an error message this time.
その他の回答 (1 件)
jatin
2024 年 9 月 19 日
clear all;
clc;
close all;
num = [0 10];
den= [0 0];
[t, y] = ode45(@ode_system,num,den)]
plot(t, y(:,1));
xlabel('Time t');
ylabel('Solution y(t)');
title('Solution of the second-order differential equation');
grid on;
end
1 件のコメント
Walter Roberson
2024 年 9 月 19 日
The user specifically asked for symbolic solution.
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
