problem with ode45 solving
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello, I'm trying to solve the following system of differential equations but I'm not getting the right answer
ode1 = diff(x) == v;
ode2 = diff(v) == u;
t_interval = [0,10]
x0 = 0
v0 = 0
u = 1
because of how things are set up I can wrap my head on creating the function. Any help would be appreciate it.
채택된 답변
Star Strider
2021년 4월 28일
Assuming both functions are functions of time —
syms x(t) v(t)
u = sym(1);
ode1 = diff(x) == v
ode1(t) =

ode2 = diff(v) == u
ode2(t) =

[x(t),v(t)] = dsolve(ode1,ode2, x(0)==0, v(0)==0)
x(t) =
t
v(t) =

figure
fplot(x, [0 10])
hold on
fplot(v, [0 1])
hold off
grid
xlabel('t')
legend('x(t)','v(t)')

.
댓글 수: 20
Juan Hurtado
2021년 4월 28일
This great thanks! I was confused on the u = sym(1) and thats why I was have trouble implemented it but this should work thanks!
Juan Hurtado
2021년 4월 29일
Also I want to change u for -sign(x) but when I do that I got the following error:
syms x(t) v(t)
u1 = sym(-sign(x));
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0);
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
hold off
grid
xlabel('t')
legend('x(t)','v(t)')
"
diff(v(t), t) == -sign(x(t))
Warning: Unable to find symbolic solution. "
What do you think its happening?
Star Strider
2021년 4월 29일
The sign call creates a nonlinear condition so a symbolic solution is not possible. (I'm replying on my phone since my ISP is in fail mode.) Consider substituting the tanh() function for 'sign'. I cannot test that just now, so experiment to see if it does what you want.
Juan Hurtado
2021년 4월 29일
Cool, thanks for your help. I tried using u = sym(-tanh(x)) and I got the same error.
As always, my pleasure!
If dsolve cannot work with it, then the only option is to integrate it numerically —
syms x(t) v(t) Y
% u1 = sym(-sign(x));
u1 = -tanh(x);
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[VF,Subs] = odeToVectorField(ode1,ode2)
VF =

Subs =

odefcn = matlabFunction(VF, 'Vars',{t,Y});
[t,y] = ode45(odefcn, [0 5], [0 0]+1E-8);
figure
plot(t,y)
grid
xlabel('t')
legend('x(t)','v(t)')

.
Juan Hurtado
2021년 4월 29일
this is great thank you so much! still wondering why sign does note work as well here. I tried it and I mean you get results but it doesn't seem to make sense.
Star Strider
2021년 4월 29일
As always, my pleasure! Thank you!
The problem is that the sign function creates abrupt discontinuities. Numerical integration functiond cannot handle those well becaues they are not differentiable. The tanh function does something similar, however since it is differentiable, the numeric integration succeeds.
Juan Hurtado
2021년 4월 29일
ohh that makes sence thank you!
One last thing. So now I want to solve this system of equations
syms x(t) v(t) a(t)
u = sym(1);
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,10];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == a;
ode3 = diff(a) == u;
[x(t),v(t),a(t)] = dsolve(ode1,ode2,ode3, x(0)==x0, v(0)==v0);
I got an answer but when I'm trying to plot on component I get the following (e.g plot(x, t_interval)) I get the following error "Data must be numeric, datetime, duration or an array convertible to double."
Do you know what it could be?
I do not see the plot call, however I assume you used plot rather than fplot.
And, ‘a’ needs an initial condition, too.
Try this —
syms x(t) v(t) a(t)
u = sym(1);
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,10];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == a;
ode3 = diff(a) == u;
[x(t),v(t),a(t)] = dsolve(ode1,ode2,ode3, x(0)==x0, v(0)==v0, a(0)==0)
x(t) =
t
v(t) =

a(t) =

figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
fplot(a, t_interval)
hold off
grid
legend('x(t)','v(t)','a(t)', 'Location','best', 'Interpreter','latex')

.
Juan Hurtado
2021년 5월 3일
ohh cool that works! One last question that I'd have would be on what would be a better way to plot the results of the simulation across different initial conditions and plot them in the same plot.
I tried running a for loop but is not working, I'm having the following error:
syms x(t) v(t)
u = sym(1);
%time interval and initial conditions
x = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,5];
for i = 1:length(v)
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x(i), v(0)==v0);
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
hold off
grid
xlabel('t')
legend('x(t)','v(t)')
end
Array indices must be positive integers or logical values.
Any help would be appreciate it.
Thank you!
This was a bit of a challenge, because of the way the Symbolic Math Toolbox works. The solution was to create a new copy of ‘v(t)’ in each iteratioon (as well as rename vector ‘x’ as ‘xv’ since ‘x’ is already defined as ‘x(t)’ and defining it as a vector later that confuses things).
syms x(t) v(t) x0
u = sym(1);
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0)
x(t) =
t
v(t) =
vx(t) = v % Copy 'v' to 'vx'
vx(t) =
for i = 1:length(xv)
v = vx % Create New Copy Of 'v' In Each Loop
ic_v = xv(i) % Display 'xv(i)'
v = subs(v,{x0},{ic_v}) % Substitute 'v(i)' For 'x0'
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
hold off
grid
ylim([-3 16])
xlabel('t')
legend('x(t)','v(t)', 'Location','best')
end
v(t) =
ic_v = -3
v(t) =

v(t) =
ic_v = -2
v(t) =

v(t) =
ic_v = -1
v(t) =

v(t) =
ic_v = 1
v(t) =

v(t) =
ic_v = 2
v(t) =

v(t) =
ic_v = 3
v(t) =

.
this is really helpfull! thank you! What if I wanted to plot all of them in the same plot? I though I would have to use this command during the for loop but is not plotting together.
hold on
As always, my pleasure!
Try this —
syms x(t) v(t) x0
u = sym(1);
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0)
x(t) =
t
v(t) =
vx(t) = v; % Copy 'v' to 'vx'
figure
hold on
for i = 1:length(xv)
v = vx; % Create New Copy Of 'v' In Each Loop
ic_v = xv(i); % Display 'xv(i)'
v = subs(v,{x0},{ic_v}); % Substitute 'v(i)' For 'x0'
fplot(x, t_interval,'-k','LineWidth',1.2)
fplot(v, t_interval)
end
hold off
grid
xlabel('t')
icv = compose('v(t) i.c. = %2d',xv);
legend(['x(t)',icv], 'Location','best')

.
Juan Hurtado
2021년 5월 3일
This is great! Thank you. I was trying to do the same with the sign function part:
syms x(t) v(t) Y
u1 = sym(-sign(x));
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,20];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[VF,Subs] = odeToVectorField(ode1,ode2);
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0);
odefcn = matlabFunction(VF, 'Vars',{t,Y});
[t,y] = ode45(odefcn, t_interval, [x0 v0]+1E-8);
vx(t) = v; % Copy 'v' to 'vx'
figure
hold on
for i = 1:length(xv)
v = vx; % Create New Copy Of 'v' In Each Loop
ic_v = xv(i); % Display 'xv(i)'
v = subs(v,{x0},{ic_v}); % Substitute 'v(i)' For 'x0'
fplot(x, t_interval,'-k','LineWidth',1.2)
fplot(v, t_interval)
end
hold off
grid
xlabel('t')
icv = compose('v(t) x0 = %2d',xv);
legend(['x(t)',icv], 'Location','best')
and I get Unrecognized function or variable 'x0'. Which makes sense since now x0 is now a vector xv but I'm kind of confused on how to reeplace x0 for each of the elements in xv.
Please do not mix symbolic and numeric calculations!
Try this —
syms x(t) v(t) Y x0
u1 = sym(-sign(x));
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,20];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[VF,Subs] = odeToVectorField(ode1,ode2);
% [x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0);
odefcn = matlabFunction(VF, 'Vars',{t,Y});
vx(t) = v; % Copy 'v' to 'vx'
tspan = double(t_interval);
figure
hold on
for i = 1:length(xv)
[t,y] = ode45(odefcn, tspan, [xv(i) 0]+1E-8);
plot(t,y)
end
hold off
grid
xlabel('t')
icv = compose('v(t) x0 = %2d',xv);
legend(['x(t)',icv], 'Location','bestoutside')

.
Juan Hurtado
2021년 5월 3일
ohhh I see now why it wasn't working, thank you again!. I have one last question:
I'm trying to run the sign function for the 3 differential equations
syms x(t) v(t) a(t)
k = 1;
u1 = sym(-sign(x+k*v));
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == a;
ode3 = diff(a) == u1;
[x(t),v(t),a(t)] = dsolve(ode1,ode2,ode3, x(0)==x0, v(0)==v0, a(0)==0);
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
fplot(a, t_interval)
hold off
grid
legend('x(t)','v(t)','a(t)', 'Location','best', 'Interpreter','latex')
But I'm having an Warning: Unable to find symbolic solution. I thing I already defining of the symbolic functions but I don't know why is not working
Juan Hurtado
2021년 5월 3일
Also I have x0=1
The sign call creates a nonlilnear function for ‘a(t)’. Most nonlinear differential equations do not have analytic solutions, and this is one of them.
Additionally, the sign function creates a discontinuity that even the numeric solvers are going to have problems with, so create a differentiable version of ‘u1’ as:
u1 = -tanh(x+k*v);
or:
u1 = @(x,k,v) -tanh(x+k*v);
and solve it numerically.
There is a recent relevant example of that in this thread, so I will not repeat it here.
To illustrate —
figure
fplot(@(x)-sign(x), [-5 5])
hold on
fplot(@(x)-tanh(5*x), [-5 5])
hold off
ylim([-1.5 1.5])
legend('-sign(x)','-tanh(5*x)', 'Location','best')
grid

.
Juan Hurtado
2021년 5월 3일
Perfect! Well I that solves all my doubts. I reall appreciate all your help.
Thank you so much!
Star Strider
2021년 5월 3일
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Equation Solving에 대해 자세히 알아보기
참고 항목
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)
