Clear Filters
Clear Filters

Solving equations in a for loop with changing variables

4 views (last 30 days)
Hi all,
Not very experienced with matlab and trying to solve for d, L, and H as a function of x changing. The assignment asks that for the first half of x, H=0.5 and d=1.9 but I haven't been able to figure out how to start the loop in the middle of those vectors. I hardcoded a little bit to try to troubleshoot my first line, but the loop skews my depth somehow. Any advice would be greatly appreciated!
  2 Comments
Stephan
Stephan on 30 Oct 2020
please insert your code - we can not copy a code from a picture...
Samantha Andeson
Samantha Andeson on 31 Oct 2020
My apologies
x=linspace(0,50,500);
d=zeros(length(x),1);
H=zeros(length(x),1);
L=zeros(length(x),1);
%d(1:length(x)/2)=1.9;
%H(1:length(x)/2)=0.5;
H(1)=0.5; d(1)=1.9;
d(2)=d(1)-(S.*x(2)); %this is what I'm trying to make the first line
d(3)=d(2)-(S.*x(3)); %of my loop do
d(4)=d(3)-(S.*x(4));
X=x(:);
if H/d>0.8
for i=1:length(X)
d(i)=d(i)-(S.*X(i)); %1 column matrix
k(i)=getwavek(1/T,d(i)); %1 row matrix
L(i)=(2*pi)./(k(i)); %1 column matrix
n(i)=1/2.*(1+((2.*k(i).*d(i))/(sinh(2.*k(i).*d(i))))); %1 row matrix
N(i)=n(:); %Thought this would help by "index exceeds # of array elements" error
H(i)=H(i-1)./sqrt((N(i).*L(i))./(N(i-1).*L(i-1))); %This is my problem
end
end

Sign in to comment.

Answers (1)

Juan Sagarduy
Juan Sagarduy on 30 Oct 2020
Hi Samantha The first line in your code in the loop is not correct. It should d(i)=d(i-1)-S.*x(i). But then the loop must start with i=2. Otherwise, MATLAB would throw an error. Good luck / Juan

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!