How to fix "Subscripted Assignment Dimension Mismatch" and other potential errors
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to implement a brute force approach where I can find the best parameters and initial conditions for two differential equations to fit to a given set of data. I have implemented most of the code so far but keep getting some small errors which I am unsure how to fix. Here is my code below:
The error "Subscripted assignment dimension mismatch" is coming from line 68 which is in bold. If anyone knows how to fix this and any other potential problems with this code it would be a great help
function [FinalNorm1 FinalNorm2 FinalNormInf] = odeProbAveNormData
tic
%Step Sizes
q1Step = 0.001;
q2Step = 0.001;
q3Step = 0.001;
q4Step = 0.001;
q5Step = 0.001;
%Values Over Which to Search, min : qxStep : max
q1 = 0.05 : q1Step : 0.55;
q2 = 0 : q2Step : 0.005;
q3 = 0.03: q3Step : 0.43;
q4 = 0.4: q4Step : 0.5;
q5 = 0: q5Step : 0.05;
%Pre-allocate Matricies For Speed
X = 1 : 1 : 6;
Norm1.cost = zeros(1, numel(q1) );
Norm2.cost = zeros(1, numel(q1) );
NormInf.cost = zeros(1, numel(q1) );
loop.Norm1 = zeros(1, numel(q2) );
loop.Norm2 = zeros(1, numel(q2) );
loop.NormInf = zeros(1, numel(q2) );
Norm1.q2index = zeros(1, numel(q1) );
Norm2.q2index = zeros(1, numel(q1) );
NormInf.q2index = zeros(1, numel(q1) );
loop.Norm1 = zeros(1, numel(q3) );
loop.Norm2 = zeros(1, numel(q3) );
loop.NormInf = zeros(1, numel(q3) );
Norm1.q3index = zeros(1, numel(q2) );
Norm2.q3index = zeros(1, numel(q2) );
NormInf.q3index = zeros(1, numel(q2) );
q4Loop.Norm1 = zeros(1, numel(q4) );
q4Loop.Norm2 = zeros(1, numel(q4) );
q4Loop.NormInf = zeros(1, numel(q4) );
indexq4.LoopNorm1 = zeros(1, numel(q3) );
indexq4.LoopNorm2 = zeros(1, numel(q3) );
indexq4.LoopNormInf = zeros(1, numel(q3) );
Norm1.q4index = zeros(1, numel(q1) );
Norm2.q4index = zeros(1, numel(q1) );
NormInf.q4index = zeros(1, numel(q1) );
q5Loop.Norm1 = zeros(1, numel(q5) );
q5Loop.Norm2 = zeros(1, numel(q5) );
q5Loop.NormInf = zeros(1, numel(q5) );
indexq5.LoopNorm1 = zeros(1, numel(q4) );
indexq5.LoopNorm2 = zeros(1, numel(q4) );
indexq5.LoopNormInf = zeros(1, numel(q4) );
Norm1.q5index = zeros(1, numel(q1) );
Norm2.q5index = zeros(1, numel(q1) );
NormInf.q5index = zeros(1, numel(q1) );
%Main Loop
for i = 1:numel(q1)
for j = 1:numel(q2)
for k = 1:numel(q3)
for l = 1:numel(q4)
for m = 1:numel(q5)
[~, Y] = ode45(@Solver, X, [q4(l),q5(m)]);
Result = Y(1:1:6);
Data.set(:,1) = [q4(l) 0.387 0.415 0.382 0.345 0.314 0.244];
Data.set(:,2) = [q5(m) 0.0041 0.0185 0.0938 0.1151 0.1388 0.1686];
%Calculate Differences
for n = 1:2
Difference.set(:,n) = (Result - Data.set(:,n)); <-- Error Line
end
%Take the Norms of Differences and Average
for n = 1:2
Norm.one(n) = norm(Difference.set(:,n),1);
Norm.two(n) = norm(Difference.set(:,n),2);
Norm.inf(n) = norm(Difference.set(:,n),inf);
end
q5Loop.Norm1(m) = mean(Norm.one);
q5Loop.Norm2(m) = mean(Norm.two);
q5Loop.NormInf(m) = mean(Norm.inf);
end
q4Loop.Norm1(l) = mean(Norm.one);
q4Loop.Norm2(l) = mean(Norm.two);
q4Loop.NormInf(l) = mean(Norm.inf);
end
%Store Cost and Value of Optimal q5
[loop.Norm1(k), indexq5Loop.Norm1(k)] = min(q5Loop.Norm1);
[loop.Norm2(k), indexq5Loop.Norm2(k)] = min(q5Loop.Norm2);
[loop.NormInf(k), indexq5Loop.NormInf(k)] = min(q5Loop.NormInf);
%Store Cost and Value of Optimal q4
[loop.Norm1(k), indexq4Loop.Norm1(k)] = min(q4Loop.Norm1);
[loop.Norm2(k), indexq4Loop.Norm2(k)] = min(q4Loop.Norm2);
[loop.NormInf(k), indexq4Loop.NormInf(k)] = min(q4Loop.NormInf);
end
%Store Cost and Value of Optimal q3 and Its Associated q4
[Norm1.cost(j), Norm1.q3index(j)] = min(loop.Norm1);
Norm1.q4index(j) = indexq4Loop.Norm1(Norm1.q3index(j));
[Norm2.cost(j), Norm2.q3index(j)] = min(loop.Norm2);
Norm2.q4index(j) = indexq4Loop.Norm2(Norm2.q3index(j));
[Norm2.cost(j), NormInf.q3index(j)] = min(loop.NormInf);
NormInf.q4index(j) = indexq4Loop.NormInf(NormInf.q3index(j));
%Store Cost and Value of Optimal q3 and Its Associated q5
[Norm1.cost(j), Norm1.q3index(j)] = min(loop.Norm1);
Norm1.q5index(j) = indexq5Loop.Norm1(Norm1.q3index(j));
[Norm2.cost(j), Norm2.q3index(j)] = min(loop.Norm2);
Norm2.q5index(j) = indexq5Loop.Norm2(Norm2.q3index(j));
[Norm2.cost(j), NormInf.q3index(j)] = min(loop.NormInf);
NormInf.q5index(j) = indexq5Loop.NormInf(NormInf.q3index(j));
end
%Store Cost and Value of Optimal q2 and Its Associated q4
[Norm1.cost(i), Norm1.q2index(i)] = min(loop.Norm1);
Norm1.q4index(i) = indexq4Loop.Norm1(Norm1.q2index(i));
[Norm2.cost(i), Norm2.q2index(i)] = min(loop.Norm2);
Norm2.q4index(i) = indexq4Loop.Norm2(Norm2.q2index(i));
[Norm2.cost(i), NormInf.q2index(i)] = min(loop.NormInf);
NormInf.q4index(i) = indexq4Loop.NormInf(NormInf.q2index(i));
%Store Cost and Value of Optimal q2 and Its Associated q5
[Norm1.cost(i), Norm1.q2index(i)] = min(loop.Norm1);
Norm1.q5index(i) = indexq5Loop.Norm1(Norm1.q2index(i));
[Norm2.cost(i), Norm2.q2index(i)] = min(loop.Norm2);
Norm2.q5index(i) = indexq5Loop.Norm2(Norm2.q2index(i));
[Norm2.cost(i), NormInf.q2index(i)] = min(loop.NormInf);
NormInf.q5index(i) = indexq5Loop.NormInf(NormInf.q2index(i));
end
%Determine Optimal q1 and Its Associated q2 and q3 Values
[C1, I1] = min(Norm1.cost);
FinalNorm1(1) = (I1 - 1) * q1Step + q1(1);
FinalNorm1(2) = (Norm1.q2index(I1) - 1) * q2Step + q2(1);
FinalNorm1(3) = (Norm1.q3index(I1) - 1) * q3Step + q3(1);
FinalNorm1(4) = (Norm1.q4index(I1) - 1) * q4Step + q4(1);
FinalNorm1(5) = (Norm1.q5index(I1) - 1) * q5Step + q5(1);
[C2,I2] = min(Norm2.cost);
FinalNorm2(1) = (I2 - 1) * q1Step + q1(1);
FinalNorm2(2) = (Norm1.q2index(I2) - 1) * q2Step + q2(1);
FinalNorm2(3) = (Norm1.q3index(I2) - 1) * q3Step + q3(1);
FinalNorm1(4) = (Norm1.q4index(I1) - 1) * q4Step + q4(1);
FinalNorm1(5) = (Norm1.q5index(I1) - 1) * q5Step + q5(1);
[CInf, IInf] = min(NormInf.cost);
FinalNormInf(1) = (IInf - 1) * q1Step + q1(1);
FinalNormInf(2) = (Norm1.q2index(IInf) - 1) * q2Step + q2(1);
FinalNormInf(3) = (Norm1.q3index(IInf) - 1) * q3Step + q3(1);
FinalNorm1(4) = (Norm1.q4index(I1) - 1) * q4Step + q4(1);
FinalNorm1(5) = (Norm1.q5index(I1) - 1) * q5Step + q5(1);
toc
function [Ddv_Div] = Solver(~, D)
% IV, I, IVsol - Independent Variables
% DV, D, DVsol - Dependent Variables
x = D(1);
y = D(2);
Ddv_Div = [ -x*q1(i) + y*q2(j) + (1-x-y)*q2(j);
-y*q2(j) + (1-x-y)*q3(k) ];
end
end
1 Kommentar
Guillaume
am 24 Mär. 2017
Note: the line that is causing an error is the last of:
[~, Y] = ode45(@Solver, X, [q4(l),q5(m)]);
Result = Y(1:1:6);
Data.set(:,1) = [q4(l) 0.387 0.415 0.382 0.345 0.314 0.244];
Data.set(:,2) = [q5(m) 0.0041 0.0185 0.0938 0.1151 0.1388 0.1686];
%Calculate Differences
for n = 1:2
Difference.set(:,n) = (Result - Data.set(:,n));
Antworten (2)
Guillaume
am 24 Mär. 2017
Bearbeitet: Guillaume
am 24 Mär. 2017
I assume you're using R2016b or later, otherwise the error you'd get would be different. Was the code written for an earlier version?
[~, Y] = ode45(@Solver, X, [q4(l),q5(m)]);
The Y variable created by this is a 6 rows x 2 column matrix. We then have:
Result = Y(1:1:6);
What is the intention behind that line? At the present, it returns the first column of Y as a row vector, so is equivalent to:
Result = Y(:, 1).'; %if that's the intention, this would be clearer
Maybe the intent was to keep just the first column of Y, in which case:
Result = Y(:, 1);
Anyway, with your code, we have Result a 1 row by 6 columns vector which may have been meant to be 6 rows by 1 column vector.
We then have:
Data.set(:,1) = [q4(l) 0.387 0.415 0.382 0.345 0.314 0.244];
Data.set(:,2) = [q5(m) 0.0041 0.0185 0.0938 0.1151 0.1388 0.1686];
Again, a bit confusing. We're copying a row vector into columns. A comment explaining that's the intent would remove any confusion or failing that being explicit with:
Data.set(:,1) = [q4(l) 0.387 0.415 0.382 0.345 0.314 0.244].'; %copy column into column
Data.set(:,2) = [q5(m) 0.0041 0.0185 0.0938 0.1151 0.1388 0.1686].';
Anyway, we have Data.Set a 7 rows by 2 columns matrix.
We then have:
Difference.set(:,n) = (Result - Data.set(:,n));
Pre-R2016b, the above would error on the subtraction (with error using -, matrix dimensions must agree) as you're trying to subtract a 7 elements vector from a 6 elements vector. Had the vectors been the same length, the result would have been assigned, with no error, as a column vector in Difference.Set
From R2016b, the subtraction succeeds because the two vectors are in different directions. One is a row vector, the other a column vector. As a result, the implicit expansion introduced in R2016b kicks in and results in a 7x6 matrix. At this point, you're trying to assign a 6 columns matrix into a single column of Difference.Set, hence you get a 'Subscripted assignment dimension mismatch' error.
All this to say that, regardless of the version, you'd get an error. If you're trying to do a memberwise subtraction you need to ensure that you have the same number of elements in each vector, and since R2016b, that they're the same shape (both row, or both column vectors).
And also: don't keep switching directions on your vectors. It'll only leads to bugs as it's confusing.
2 Kommentare
dpb
am 24 Mär. 2017
Another case where silent expansion leads to totally unexpected result...I can't come to grips with this "enhancement"; I noticed it and should've pointed it out in the Answer below but I really wasn't sure just what it would do with it...what logic there is in producing 7x6 here escapes me.
dpb
am 24 Mär. 2017
Result = Y(1:1:6);
Data.set(:,1) = [q4(l) 0.387 0.415 0.382 0.345 0.314 0.244];
Data.set(:,2) = [q5(m) 0.0041 0.0185 0.0938 0.1151 0.1388 0.1686];
...
Difference.set(:,n) = (Result - Data.set(:,n)); <-- Error Line
...
length(Result)==6 while there are seven, count' em 7 elements in the Data.set vectors. Why?
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!