Matrix dimensions must agree

조회 수: 1 (최근 30일)
Kolleggerm1
Kolleggerm1 2019년 10월 8일
댓글: Star Strider 2019년 10월 9일
I am using Forward Euler to solve based on the given condtions that H'=(S-S(f))/(1-F^2) F^2=(q^2)/(gH^3) S(f)=C(f)F^2 and H(x(1))=H(1).
I am getting the error "Matrix dimensions must agree" for line 17.
What does that mean?
clc; clear all;
h=1000; % step size
xmin=-10000;
xmax=10000;
x=xmin:h:xmax; %x boundaries
n=length(x);
y=zeros(1,n);% Initial condition
x0=0;
q=10;
s=0.00025;
H(1)=30;
Cf=.00200;
g=9.8;
i=1:n-1;
F=sqrt((q^2)/(g*H^3));
s(i+1)=Cf*(F);
dH=(s-s(i+1))/((1-F));
for i=1:n-1
x(i+1)=i*h;
y(i+1)=y(i)+h*dH;
end
figure
hold on
plot (x,y,'r', 'linewidth',2)
xlabel ('Channel Distance(m)')
ylabel ('Water Elevation(m)')
title ('Water Elevation vs Channel Distance')
  댓글 수: 3
Star Strider
Star Strider 2019년 10월 9일
Kolleggerm1’s Answers moved here —
First:
h=1000;
xmin=-10000;
xmax=10000;
x=xmin:h:xmax;
n=length(x);
H=zeros(1,n);
x0=0;
q=10;
s=0.00025;
H(1)=30;
Cf=.00200;
g=9.81;
i=1:n-1;
slope=(s-(Cf(q.^2)/(g(H(i).^3))))/(1-((q.^2)/(g(H(i).^3))));
for i=1:n-1
x(i+1)=x(i)-h;
H=H(i-1)-slope*h;
end
figure
hold on
plot (x,H,'r', 'linewidth',2)
xlabel ('Channel Distance(m)')
ylabel ('Water Elevation(m)')
title ('Water Elevation vs Channel Distance')
Second:
I was able to fix the original error and am now getting this one (for the above code)
Index exceeds the number of array elements (1).
Error in Alltogether (line 15)
slope=(s-(Cf(q.^2)/(g(H(i).^3))))/(1-((q.^2)/(g(H(i).^3))));
Star Strider
Star Strider 2019년 10월 9일
You are missing a number of (probably multiplication) operators here:
slope=(s-(Cf(q.^2)/(g(H(i).^3))))/(1-((q.^2)/(g(H(i).^3))));
I used this:
slope=(s-(Cf*(q.^2)./(g*(H(i).^3))))./(1-((q.^2)./(g*(H(i).^3))));
that eliminated that error, however your code now has others with respect to ‘x’ and ‘H’ not having equal lengths in the plot call.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Direction of Arrival Estimation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by