Plotting 3d ODE

32 views (last 30 days)
matlab_day
matlab_day on 28 Jan 2021
Commented: Star Strider on 2 Oct 2021
I have to following code designed to plot and ODE system of 3 variables with respect to t.
t_start = 0; t_end = 50000; %set suitable time range
t_duration = [t_start t_end]; %create vector for time
[w(:,1), w(:,2), w(:,3)] = meshgrid([-1:0.1:1;],[-1:0.1:1;], [-1:.1, 1]); %create grid
w_initial = [-1; .1; -.1]; %column vector containing initial values
[t, w] = ode23(@thefunc, t_duration, w_initial);
surf(w(:,1),w(:,2), w(:,3));
function wdot = thefunc(t, w)
a = .5;
b = .4;
c = -.75;
c_0 = -1;
A = .5; %greek variable alpha
wdot = zeros(3, 1);
wdot = [a*w(2)*w(3) - A*w(1).^3;
b*w(3)*w(1) - A*w(2).^3;
c*w(1)*w(2) + w(1)*w(2) - A*w(3) - w(1)*w(2)*(-c_0.^2*b*w(1) - c_0.^2*a*w(2).^2 + c_0*A*w(1)^2 + c_0*A*w(2).^2 - c_0*A+ c)];
end
I get the error message Unable to perform assignment because the size of the left side is 9977-by-1 and the
size of the right side is 21-by-21-by-3. in the line with the meshgrid, and I'm not sure how to fix it.

Answers (1)

Star Strider
Star Strider on 28 Jan 2021
First, delete the meshgrid call, since it is not necessary here. The ode23 call produces vectors, not a surface.
Second, plot this instead:
figure
stem3(w(:,1),w(:,2), w(:,3), '.');
grid on
xlabel('w_1(t)')
ylabel('w_2(t)')
zlabel('w_3(t)')
The stem3 plot gives a bit more information than plot3 would.
.
  1 Comment
Star Strider
Star Strider on 2 Oct 2021
UPDATE — (2 Oct 2021 at 10:46)
With the online Run feature now enabled (not available until June 2021), the code becomes —
t_start = 0; t_end = 50000; %set suitable time range
t_duration = [t_start t_end]; %create vector for time
w_initial = [-1; .1; -.1]; %column vector containing initial values
[t, w] = ode23(@thefunc, t_duration, w_initial);
figure
stem3(w(:,1),w(:,2), w(:,3), '.');
grid on
xlabel('w_1(t)')
ylabel('w_2(t)')
zlabel('w_3(t)')
function wdot = thefunc(t, w)
a = .5;
b = .4;
c = -.75;
c_0 = -1;
A = .5; %greek variable alpha
wdot = zeros(3, 1);
wdot = [a*w(2)*w(3) - A*w(1).^3;
b*w(3)*w(1) - A*w(2).^3;
c*w(1)*w(2) + w(1)*w(2) - A*w(3) - w(1)*w(2)*(-c_0.^2*b*w(1) - c_0.^2*a*w(2).^2 + c_0*A*w(1)^2 + c_0*A*w(2).^2 - c_0*A+ c)];
end
With the desired 3D plot.
.

Sign in to comment.

Categories

Find more on Labels and Annotations 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!