Index exceeds matrix dimensions.
显示 更早的评论
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 个评论
Walter Roberson
2017-9-23
Please post the complete error message including the line that the problem is happening on.
Shifo
2017-9-23
回答(3 个)
Walter Roberson
2017-9-23
1 个投票
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
2017-9-23
...
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 个评论
Shifo
2017-9-23
Shifo
2017-9-24
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?
Image Analyst
2017-9-24
编辑:dpb
2017-9-24
1 个投票
Maybe you should learn about and use the linspace() function instead of using the colon operator.
9 个评论
Fixed typo of 'colon' for 'color', IA, but while linspace could get him past his "number of elements in a vector" problem, the actual solution to his problem is to use fzero to find the original solutions then do something similar probably to find the final result he's looking for. That may be, if the crystal ball is functioning correctly this morning, an inverse interpolation problem...
Unfortunately, he's so far not yet shown us the paper/text from which Eq. 10.7.6 comes so we don't know the end objective (and I don't recognize the correlation from the equations presented so far, anyway).
Shifo
2017-9-24
Shifo
2017-9-24
dpb
2017-9-25
Well, you stopped at the intriguing part of "the evaluation of conditions a location i+1 proceeds as follows:"
Looks like there would be the key to how the text did it.
I'd think if knew just what/how your variables coincided with those in the text that one could use the diff eq solvers in Matlab to solve 10.7.4 directly and then compute the desired quantities from that solution but I admit without some more context I have no idea what this all is about or what you're trying to achieve as an end result so not sure where to go, precisely.
What are the two y values we worked so hard to compute above and how do they bear on any of the above???
"Here is the another a code which is really what i want, I have found this online but cant understand it or even use it"
That seems self-contradictory, unless what you want is code that you cannot use. In any case, see also this question:
Shifo
2017-9-26
Shifo
2017-9-26
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
2017-9-26
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
