Matrix dimensions must agree

1 view (last 30 days)
Kolleggerm1
Kolleggerm1 on 8 Oct 2019
Commented: Star Strider on 9 Oct 2019
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 Comments
Star Strider
Star Strider on 9 Oct 2019
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 on 9 Oct 2019
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.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!