You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
problem with ode45 solving
1 view (last 30 days)
Show older comments
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.
Accepted Answer
Star Strider
on 28 Apr 2021
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 Comments
Juan Hurtado
on 28 Apr 2021
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
on 29 Apr 2021
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
on 29 Apr 2021
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
on 29 Apr 2021
Cool, thanks for your help. I tried using u = sym(-tanh(x)) and I got the same error.
Star Strider
on 29 Apr 2021
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
on 29 Apr 2021
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
on 29 Apr 2021
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
on 29 Apr 2021
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
on 30 Apr 2021
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
on 3 May 2021
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
on 3 May 2021
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
on 3 May 2021
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
on 3 May 2021
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
on 3 May 2021
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
on 3 May 2021
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
on 3 May 2021
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
on 3 May 2021
Also I have x0=1
Star Strider
on 3 May 2021
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
on 3 May 2021
Perfect! Well I that solves all my doubts. I reall appreciate all your help.
Thank you so much!
Star Strider
on 3 May 2021
As always, my pleasure!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)