Plotting Solution Curve on Direction Field
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
I have the second-order differential equation with initial conditions: y'' + 2y' + y = 0, y(-1) = 0, y'(0) = 0
I need to plot the direction field of the solution to the equation and trace the solution curve corresponding to the initial conditions.
I have created the direction field but I'm not sure how to plot the solution curve over the direction field.
Using the plot() function is giving errors and the ezplot() function doesn't seem to represent what the direction field is showing.
Here is what I have so far
% Finds solution to the DE
syms y(x)
Dy = diff(y);
D2y = diff(y,2);
ode = D2y + 2*Dy + y == 0;
ySol1 = dsolve(ode, y(-1)==0); % Solution to DE applying first initial conditions.
ySol2 = dsolve(ode,Dy(0)==0); % Solution to DE applying second initial conditions.
% 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([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol1)
plot(ySol2)
hold off
Any help is greatly appreciated.
採用された回答
Torsten
2022 年 4 月 13 日
ySol = dsolve(ode, [y(-1)==0,Dy(0)==0]);
dySol = diff(ySol,x);
instead of
ySol1 = dsolve(ode, y(-1)==0); % Solution to DE applying first initial conditions.
ySol2 = dsolve(ode,Dy(0)==0); % Solution to DE applying second initial conditions.
and
plot(double(subs(ySol,x,-4:0.5:4)),double(subs(dySol,x,-4:0.5:4)))
instead of
plot(ySol1)
plot(ySol2)
16 件のコメント
Jordan Stanley
2022 年 4 月 13 日
Just to understand what you did.
You solved the DE equation with the conditions and assigned it to ySol then took the derivative of that and assigned it to dySol.
Then plotted both ySol and dySol.
Is that correct?
Torsten
2022 年 4 月 13 日
Yes. This was the task, wasn't it ?
Yes, I just wanted to understand the process.
Also, when I replaced my code with your suggestion I get these errors.
Entries in second argument must be scalar.
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
G = mupadsubs(F,X,Y);
Do you see anything in my update code that might be causing these.
% 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);
% 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([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(double(subs(ySol,x,-4:0.5:4)),double(subs(dySol,x,-4:0.5:4)))
hold off
Torsten
2022 年 4 月 14 日
Then try
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 = matlabFunction(ySol);
dySol = matlabFunction(dySol);
% 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([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4));
hold off
Jordan Stanley
2022 年 4 月 14 日
Hello again,
There are less errors now but the only error I get now is,
Not enough input arguments.
Error in symengine>@(C1,x)exp(-x).*(C1+C1.*x)
I have the same code as above.
I can't run the code, but I'm surprised that ySol should still have a free constant although 2 conditions are given that the solution should fulfill.
What do you get for ySol from the command
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0]);
I solved your equation with paper and pencil and get
ySol(x) = a*exp(-x)*(1+x)
for arbitrary a.
So you can't plot anything here because the solution is not unique.
Jordan Stanley
2022 年 4 月 14 日
I get an error message that says... "Array indices must be positive integers or logical values.".
Jordan Stanley
2022 年 4 月 14 日
Interesting, well thank you for your help.
What I want to say is: You can't trace a solution curve since the solution is not unique.
What you can do is choose a special solution:
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([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4));
hold off
Jordan Stanley
2022 年 4 月 14 日
Ah okay, I believe that is what I needed.
Thank you very much.
Torsten
2022 年 4 月 14 日
And what is ySol you get from MATLAB from the line
ySol = dsolve(ode,[y(-1)==0,Dy(0)==0]);
?
Jordan Stanley
2022 年 4 月 14 日
I still get the error message, "Array indices must be positive integers or logical values."
Torsten
2022 年 4 月 14 日
And with
syms y(x)
Dy = diff(y,x);
D2y = diff(y,x,2);
ode = D2y + 2*Dy + y == 0;
ySol(x) = dsolve(ode,[y(-1)==0,Dy(0)==0])
What do you get for ySol ?
Jordan Stanley
2022 年 4 月 14 日
I get...
ySol(x) =
exp(-x)*(C1 + C1*x)
Torsten
2022 年 4 月 14 日
Then the following code should work (without prescribing ySol as I did before, but the result should be the same):
syms y(x)
Dy = diff(y,x);
D2y = diff(y,x,2);
ode = D2y + 2*Dy + y == 0;
ySol(x) = dsolve(ode,[y(-1)==0,Dy(0)==0])
ySol = subs(ySol,C1,1);
dySol = diff(ySol,x);
ySol = matlabFunction(ySol);
dySol = matlabFunction(dySol);
% 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([-3.5 3.5 -3.5 3.5]);
% Prints the solution curve corresponding to the initial conditions.
hold on
plot(ySol(-4:0.5:4),dySol(-4:0.5:4));
hold off
Jordan Stanley
2022 年 4 月 15 日
Thank you very much for the help.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Lengths and Angles についてさらに検索
製品
タグ
参考
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)
