ODE45 output in command window different to workspace
4 views (last 30 days)
Show older comments
Hi,
I am trying to integrate a system of differential equations, and when I run the code I get a slightly confusing output. The code is as given below:
function dx=multinode(t,x,Ng,K,P,alpha)
theta(1,:) = x(1:Ng);
omega(1,:) = x(Ng+1:2*Ng);
for i=1:Ng
for j = 1:Ng
thetadiff(i,j) = theta(i)-theta(j);
end
end
dtheta = omega;
domega = (P - alpha.*omega + sum(K*sin(thetadiff),2)');
dx = [dtheta domega];
dx = dx(:);
end
P = [1 1 1 -1];
alpha = [0.1, 0.1, 0.1, 0.1];
Ng = 4;
K = [0 1 1 0; 1 0 0 0; 1 0 0 1; 0 0 1 0];
K = (30)*K;
time = [0:1:100];
n = 10;
stablepoints1 = zeros(n);
thetainit = linspace(0,2*pi,n)';
omegainit = linspace(-100,100,n)';
for i = 1:n
for j = 1:n
[t,x] = ode45(@(t,x) multinode(t,x,Ng,K,P,alpha), time, [thetainit(j), pi/3, pi/3, pi/3, omegainit(i), 0, 0, 0]);
omega1out = x(end,5);
if (-0.01<omega1out(end)) && (omega1out(end))<0.01
stablepoints1(i,j) = 1;
else
stablepoints1(i,j) = 0;
end
end
end
I put a break into the for loop to see what is happening. When I type x into the command window after the first integration I get a matrix with the final row
0.8213 -0.2908 1.9860 -0.3945 -0.0094 -0.0095 0.0294 -0.0087
But when I click on x in the workspace the final row is
821.317183994671 -290.798389258562 1985.97687891989 -394.531277262133 -9.35808642795707 -9.48223176361757 29.3617184223381 -8.69923848306632.
I know that the first matrix that appears when I type x into the command window is the correct one that I would like to use in the if statement. However the code seems to use the strange matrix that appears in the workspace. Can anyone help me to understand what is going wrong here and how I can fix it?
Thanks in advance :)
0 Comments
Accepted Answer
Walter Roberson
on 5 Sep 2020
In the command window, give the command
format long g
and then display x again. You will see the same results as in the workspace.
What is happening is that you currently have format short in effect, and you overlooked that the first line of output of x said
1.0e+03 *
indicating that you have to mentally multiply all of the results on the screen by 1000 to get the actual values.
This 1.0e+03 * (or whatever shows up) is done to save space on output.
0 Comments
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!