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 个评论
the cyclist
2022-12-2
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?
Timothy Dunning
2022-12-2
Timothy Dunning
2022-12-2
Askic V
2022-12-7
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
2022-12-7
移动:the cyclist
2022-12-7
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
2022-12-7
Just want to add that you can use function isrow and iscolumn to validate what MATLAB defines as row and column vectors respectively.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!