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
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.
.
I see. Thank you for the response.
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

(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 ?
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.
I believe that I just need to graph the solution of the equation found symbolically but not necessilary on the directional field.
The title of your last query was "Plotting Solution Curve on Direction Field" ...
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
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
