Error using vertcat Dimensions of arrays being concatenated are not consistent. PLEASE HELP

1 view (last 30 days)
I have this code and it show me an error as in the title of the question
function [] = call_func
clear all
close all
tspan = [ 0 15 ]
y0 = 0:0.01:1;
x0 = 0:0.01:1;
theta0 = 0:0.1*pi:2*pi;
l = 1;
options=odeset('RelTol',1e-8,'AbsTol',1e-8)
for k = 1:length(y0)
for s = 1:length(x0)
for w = 1:length(theta0)
[t,x] = ode45(@func,tspan,[x0(s) y0(k) theta0(w)],options);
%legend('b = blue', 'r = red')
if (max(mod(x(:,1),l))<0.5) && (min(mod(x(:,2),l))>0.5) || (max(mod(x(:,1),l))<0.5) && (max(mod(x(:,2),l))<0.5) || (min(mod(x(:,1),l))>0.5) && (max(mod(x(:,2),l))<0.5) || (min(mod(x(:,1),l))>0.5) && (min(mod(x(:,2),l))>0.5)
c='r.';
else
c='b.';
end
plot(mod(x(end,1),l),mod(x(end,2),l),c);
hold on
end
end
end
end
function dydt = func(t,x)
Vs = 0.0;% true value
alpha =1; % true value
B = 0.12;
w = 6.28;
tau = 0;
L = 1;
dydt = [sin(2*pi*(x(1)-(B/L)*sin(w*tau*t))*cos(2*pi*x(2)) +Vs*cos(x(3)))
cos(2*pi*(x(1)-(B/L)*sin(w*tau*t)))*sin(2*pi*x(2)) +Vs*sin(x(3))
2*pi*alpha*sin(2*x(3))*cos(2*pi*(x(1)-B/L*sin(w*tau*t)))*cos(2*pi*x(2))+2*pi*sin(2*pi*(x(1)-B/L*sin(w*tau*t)))*sin(2*pi*x(2))];
end

Accepted Answer

Star Strider
Star Strider on 7 Apr 2021
Edited: Star Strider on 7 Apr 2021
The problem is in the expression for ‘dydt’, since MATLAB interprets the spaces as delimiters. This will throw the reported error. The easiest way to correct that is to put parentheses around each row, so MATLAB considers each row a distinct expression:
dydt = [(sin(2*pi*(x(1)-(B/L))*sin(w*tau*t))*cos(2*pi*x(2)) +Vs*cos(x(3)))
(cos(2*pi*(x(1)-(B/L)*sin(w*tau*t)))*sin(2*pi*x(2)) +Vs*sin(x(3)))
(2*pi*alpha*sin(2*x(3))*cos(2*pi*(x(1)-B/L*sin(w*tau*t)))*cos(2*pi*x(2))+2*pi*sin(2*pi*(x(1)-B/L*sin(w*tau*t)))*sin(2*pi*x(2)))];
There was also a missing parenthesis enclosing the second sin call in the first row. This version of ‘dydt’ runs without error, however it would be advisable to check to be certain that I put the correct closing parenthesis in the correct location.
Beyond that, the code takes forever to execute. It might be better to store the values you want to plot in vectors and then plot them at the end, rather than plotting them in the loop.
EDIT — (7 Apr 2021 at 16:54)
The code reported this run time:
Elapsed time is 918.396917 seconds.
and produced this plot:
.
  4 Comments
Ahmed Sulaiman Ahmed Al Rawahi
Hello Star Strider,
I hope you are doing well. Sorry to send this late, but I tried to run the code and it show me " Unrecognized variable y0". I want help running the code please.
Here is the code I have,
function [] = call_func
clear all
close all
numrows = numel(y0)*numel(x0)*numel(theta0);
xmtx = zeros(numrows,2);
cmtx = zeros(numrows,3);
k = 0;
for k = 1:length(y0)
for s = 1:length(x0)
for w = 1:length(theta0)
k = k + 1;
[t,x] = ode45(@func,tspan,[x0(s) y0(k) theta0(w)],options);
%legend('b = blue', 'r = red')
if (max(mod(x(:,1),l))<0.5) && (min(mod(x(:,2),l))>0.5) || (max(mod(x(:,1),l))<0.5) && (max(mod(x(:,2),l))<0.5) || (min(mod(x(:,1),l))>0.5) && (max(mod(x(:,2),l))<0.5) || (min(mod(x(:,1),l))>0.5) && (min(mod(x(:,2),l))>0.5)
cv = [1 0 0];
% c='r.';
else
cv = [0 0 1];
% c='b.';
end
cmtx(k,:) = cv;
xmtx(k,:) = [mod(x(end,1),l), mod(x(end,2),l)];
% hold on
end
end
end
figure
scatter(xmtx(:,1), xmtx(:,2), 5, cmtx, 'filled')
end
function dydt = func(t,x)
Vs = 0.05;
alpha =1;
B = 0.12;
w = 6.28;
tau = 80;
L = 1;
dydt = [(sin(2*pi*(x(1)-(B/L)*sin(w*tau*t))*cos(2*pi*x(2)) +Vs*cos(x(3))))
( cos(2*pi*(x(1)-(B/L)*sin(w*tau*t)))*sin(2*pi*x(2)) +Vs*sin(x(3)))
(2*pi*alpha*sin(2*x(3))*cos(2*pi*(x(1)-B/L*sin(w*tau*t)))*cos(2*pi*x(2))+2*pi*sin(2*pi*(x(1)-B/L*sin(w*tau*t)))*sin(2*pi*x(2)))];
end
Star Strider
Star Strider on 14 Apr 2021
The ‘y0’ vector is in the original posted code:
y0 = 0:0.01:1;
and this ode45 call simply selects one element of it as part of the initial conditions vector:
[t,x] = ode45(@func,tspan,[x0(s) y0(k) theta0(w)],options);
exactly as in the code originally posted.
Import relevant parts of the original code into the function (or pass them as arguments to it) and the code should run without error.
The only changes I made were to change ‘dydt’ so it would run without error, introduce the ‘k’ counting variable, and preallocate the matrices that reduced the run time by >20%.
Also, these:
clear all
close all
have no effect inside the function, since the function has its own workspace that is automatically cleared (since there are no persistent variables) whenever it is called.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!