Filter löschen
Filter löschen

Solving equations in a for loop with changing variables

4 Ansichten (letzte 30 Tage)
Samantha Andeson
Samantha Andeson am 30 Okt. 2020
Kommentiert: Samantha Andeson am 31 Okt. 2020
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 Kommentare
Stephan
Stephan am 30 Okt. 2020
please insert your code - we can not copy a code from a picture...
Samantha Andeson
Samantha Andeson am 31 Okt. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Juan Sagarduy
Juan Sagarduy am 30 Okt. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by