Index exceeds matrix dimensions.

7 vues (au cours des 30 derniers jours)
Shifo
Shifo le 23 Sep 2017
Commenté : Shifo le 26 Sep 2017
clear;
%Input data
Q=22;
Z=2.5;
b=7.5;
L=3000;
So=0.0006;
n=0.015;
g=9.81;
%Set range and increments of y
y=0.01:.001:5;
%Define Functions
A=b*y+Z*y.^2;
B=b+2*Z*y;
P=b+2*y*sqrt(1+Z^2);
S=Q^2*n^2./(A.^3.3333.*P.^-1.3333);
Fr=sqrt(Q^2*B./(g*A.^3));
%Find values of y where (s-So) and (Fr-1) cross zero
[S,t]=min(abs(S-So));
yN=y(t)
[S,t]=min(abs(Fr-1));
yc=y(t)
%Reset range and increments of y to go from critical
%to normal
clear y;
dy=0.1;
y=yc:dy:yN
%Calculate specific energy at new y values
A=b*y+Z*y.^2;
E=y+Q^2./(2*g*A.^2)
%Calculate x locations according to Eq.10.7.6
ym=(y(2:5)+y(1:4))/2;
A_ym=b*ym+Z*ym.^2;
P_ym=b+2*ym*sqrt(1+Z^2);
S_ym=Q^2*n^2./(A_ym.^(10/3).*P_ym.^(-4/3));
x(1)=L;
for i=2:5;
x(i)=x(i-1)+(E(i)-E(i-1))/(So-S_ym(i-1));
end;
  2 commentaires
Walter Roberson
Walter Roberson le 23 Sep 2017
Please post the complete error message including the line that the problem is happening on.
Shifo
Shifo le 23 Sep 2017
Index exceeds matrix dimensions.
Error in Q3 (line 33) ym=(y(2:100)+y(1:99))/2;
Here is the error

Connectez-vous pour commenter.

Réponses (3)

Walter Roberson
Walter Roberson le 23 Sep 2017
Your yN and yc are extracted from the 101-element long y=0:.1:10 , so if you were to do yc:.1:yN the absolute best you could hope for would be 101 elements but because of the way you determine yc and yN, yc:.1:yN would almost certainly be shorter than 100. But you do not use .1 as the increment: you do yc:.2:yN so even if yc turned out to be y(1) and yN turned out to be y(end), the longest that yc:.2:yN could possibly be would be 51 elements. You then try to index that "at most 51 long" vector at element number 100.

dpb
dpb le 23 Sep 2017
...
yN =
0.4000
yc =
0.3000
y =
0.3000
E =
0.3844
Index exceeds matrix dimensions.
You've got all the debugging info you need; you defined
y=yc:dy:yN;
which is the same as
y=0.3:0.2:0.4;
so as the debugging info you printed shows, y turns out to be just yc because
yc+dy > yN
so the expansion is satisfied after the first element.
>> whos y
Name Size Bytes Class Attributes
y 1x1 8 double
>>
You subsequently expect to have at least 200 elements in y.
Not sure just what you really are intending here, but that's why it fails. How to set the two limits to what really want or whether those are right but you're just expecting something different and dy should be much smaller or just not have as many elements is something you'll have to decide based on what it is you're actually trying to do.
  4 commentaires
Shifo
Shifo le 24 Sep 2017
Any help guys?
dpb
dpb le 24 Sep 2017
Modifié(e) : dpb le 24 Sep 2017
The underlying real problem definition is still lacking, but the first step could be
function y=shifo
Q=22;
Z=2.5;
b=7.5;
L=3000;
So=0.0006;
n=0.015;
g=9.81;
function S=fnS(y)
A=b*y+Z*y.^2;
P=b+2*y*sqrt(1+Z^2);
S=Q^2*n^2./(A.^3.3333.*P.^-1.3333);
end
function Fr=fnFr(y)
A=b*y+Z*y.^2;
B=b+2*Z*y;
Fr=sqrt(Q^2*B./(g*A.^3));
end
%Find values of y where (s-So) and (Fr-1) cross zero
yS=fzero(@(y) fnS(y)-So,1);
yF=fzero(@(y) fnFr(y)-1,1);
y=[yS;yF];
end
where I've used internal functions so the two calculating functions have access to all the constants without passing them directly or duplicating them in code; in later releases you can also include functions in a script file; can't yet w/ R2014b which is what I have...anyway, those logistics aside, the above finds the two y locations for the two functions directly -- the question is precisely what to do next. I'll note part of the difficulty in your original code is the reuse of y for different variables; keep the two solutions separate until you're done with them.
The above yields--
>> shifo
ans =
1.2916
0.8648
>>
for yS, yFr respectively.
Is there supposed to be a minimum or something else somewhere between these two points I gather? If so, what is it specifically you're trying to solve for next as end result?

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 24 Sep 2017
Modifié(e) : dpb le 24 Sep 2017
Maybe you should learn about and use the linspace() function instead of using the colon operator.
  9 commentaires
Stephen23
Stephen23 le 26 Sep 2017
Modifié(e) : Stephen23 le 26 Sep 2017
Someone wrote a Newton-Raphson root finder with fifteen calls to eval ?! As a joke, I presume.
@Shifo: your posts and questions are a huge waste of your own time. After several days of posting useless code and random textbook excerpts, we are still none-the-wiser to what you are actually trying to do. Only a few minutes ago did you post the first hint of an explanation... You should read this:
Shifo
Shifo le 26 Sep 2017
I completely agree with you Stephen, I dont have that much experience in matlab but I am learning specially from you guys.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by