incompatible array sizes for 2x1 column vectors

I have an array, z, with 2 rows and an iteratively expanding amount of columns the first of which is assigned: z(:,1) = [0,2500];
I pass the current (nth number) value of the array with z = ivpsolver(z(:,n)) and then again with z(:,n+1) = rungekutta(z(:,n));
in this function I get an error on a line that says: znext = z + (1/6)*(Az+2*Bz+2*Cz+Dz); where Az, Bz, Cz and Dz are 1x2 column vectors (as z should be). The error states "Arrays have incompatible sizes for this operation."
am I missing something obvious or is the error likely somewhere else?

6 Kommentare

You say that "1x2 column vectors", but a 1x2 would be a row vector. Did you mean 2x1? Was that the error?
It can be difficult to diagnose a coding error from just a description of what is going on, rather than seeing the actual code. Can you post the code, or a snippet we can run that will reproduce the error?
you're right, i meant 2x1 as in the code z(:,1) = [0,2500] (as in all rows in column 1 are declared - telll me if i'm wrong here). Ill just fiddle around and try to isolate the error so i can post it here.
Here are the functions, decided to just post the whole thing (not sure if that's bad etiquete, sorry). the first function should be called with:
[n,t,z,q] = ivpSolver(1,[0,2500],[0,0],0.25,90)
function [n,t,z,q] = ivpSolver(n,t,z,q,dt,theta)
% ivpSolver Solve an initial value problem (IVP) and plot the result
%
% [T,Z] = ivpSolver(T0,Z0,DT,TE) computes the IVP solution using a step
% size DT, beginning at time T0 and initial state Z0 and ending at time
% TEND. The solution is output as a time vector T and a matrix of state
% vectors Z.
% Set initial conditions
M(1)=25000;
% Continue stepping until the end time is exceeded
while z(1,n)>=0 || z(2,n)>0
% Increment the time vector by one time step
t(n+1) = t(n) + dt;
%Apply RK4 method for one time step
[z(:,n+1),q(:,n+1)] = stepRungeKutta(z(:,n), q(:,n), dt, M(n), theta);
if M(n)>0.5
M(n+1)=M(n)-(dM*dt);
else
M(n+1)=M(n);
end
n = n + 1;
end
function [znext, qnext] = stepRungeKutta(z,q,dt,M, theta)
% Calculate the state derivative from the current state
[dz,dq] = stateDeriv2(z, q, M, theta);
% calculate individual approximations from 1st, 2nd, 3rd and 4th order
% averages
Az = dt*(dz);
Bz= dt*(dz+(Az/2));
Cz= dt*(dz+(Bz/2));
Dz= dt*(dz+Cz);
Aq = dt*(dq);
Bq= dt*(dq+(Aq/2));
Cq= dt*(dq+(Bq/2));
Dq= dt*(dq+Cq);
% combine averages for the next value approximation
znext = z + (1/6)*(Az+2*Bz+2*Cz+Dz);
qnext = q + (1/6)*(Aq+2*Bq+2*Cq+Dq);
function [dz, dq] = stateDeriv2(z,q,m,theta)
% Calculate the state derivative for a mass-spring-damper system
%
% DZ = stateDeriv(T,Z) computes the derivative DZ = [V; A] of the
% state vector Z = [X; V], where X is displacement, V is velocity,
% and A is acceleration.
Cp = 1.2;
Cc = 0.1;
Ac = pi;
Ap = 6 * 4;
G = 6.674e-11;
M = 5.972e24;
R = 6378e3;
rho = atmosEarth(z);
theta = theta*pi/180;
if z(2,:)>0 || z(1,:)>0
dz1 = z(2,:);
dz2 = (-M*G)/((R+z(1,:))^2)-(sin(theta)*(Cc*Ac+Cp*Ap)*rho*(z(2,:)^2))/(2*m);
dq1 = q(2,:);
dq2 = (-cos(theta)*(Cc*Ac+Cp*Ap)*rho*(q(2,:)^2))/(2*m);
else
dz1 = z(2,:);
dz2 = (-M*G)/((R+z(1,:))^2)-(sin(theta)*Cc*Ac*rho*(z(2,:)^2))/(2*m);
dq1 = q(2,:);
dq2 = (-cos(theta)*Cc*Ac*rho*(q(2,:)^2))/(2*m);
end
dz = [dz1; dz2];
dq = [dq1; dq2];
First of all you are calling the the first function in the following way:
[n,t,z,q] = ivpSolver(1,[0,2500],[0,0],0.25,90)
Bu the function is defined like this:
function [n,t,z,q] = ivpSolver(n,t,z,q,dt,theta)
So, the function expects 6 input arguments when called and you make a call with 5 arguments.
When function starts to execute, it will have the following variables inside its scope:
Name Size Bytes Class Attributes
dt 1x1 8 double
n 1x1 8 double
q 1x1 8 double
t 1x2 16 double
z 1x2 16 double
Therefore, not theta is defined. Later, in ivpSolver function you have a line of code:
[z(:,n+1),q(:,n+1)] = stepRungeKutta(z(:,n), q(:,n), dt, M(n), theta);
Where you call another function supplying theta as an input parameter, and that will generate the first error.
One more thing, if you have a column, it is very unusual to say "as in all rows in column 1 are declared". The column has elements and its size is Nx1, so I would normally expect
z(:, 1) = [0; 2500] % and not z(:, 1) = [0, 2500]
Gokul Nath S J
Gokul Nath S J am 7 Dez. 2022
Verschoben: the cyclist am 7 Dez. 2022
Hi Timothy Dunning,
As per my understanding, you are calling ivpSolver with five arguments while the function demands for six of them. Check whether all the arguments are properly passed to the function.
Further, the entity [0, 2500] is a row vector of dimension 1 x 2. If your requirement is a 2 x 1 column vector, you might need to specify it as [0; 2500].
Thomas
Thomas am 7 Dez. 2022
Just want to add that you can use function isrow and iscolumn to validate what MATLAB defines as row and column vectors respectively.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Timothy Dunning
Timothy Dunning am 7 Dez. 2022

0 Stimmen

Thanks all, you were all right, i was passing the wrong number of variables but I also had another issue elsewhere that was causing the array, z, to be 1x3 instead of 1x2

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by