problem with ode45 solving
1 次查看(过去 30 天)
显示 更早的评论
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
ode2 = diff(v) == u
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==0, v(0)==0)
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.
Star Strider
2021-4-29
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?
Star Strider
2021-4-30
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.
Star Strider
2021-5-3
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) =
.
Juan Hurtado
2021-5-3
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
Star Strider
2021-5-3
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.
Star Strider
2021-5-3
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
Star Strider
2021-5-3
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!
更多回答(0 个)
另请参阅
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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)