Solving equations in a for loop with changing variables

8 ビュー (過去 30 日間)
Samantha Andeson
Samantha Andeson 2020 年 10 月 30 日
コメント済み: Samantha Andeson 2020 年 10 月 31 日
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 件のコメント
Stephan
Stephan 2020 年 10 月 30 日
please insert your code - we can not copy a code from a picture...
Samantha Andeson
Samantha Andeson 2020 年 10 月 31 日
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

サインインしてコメントする。

回答 (1 件)

Juan Sagarduy
Juan Sagarduy 2020 年 10 月 30 日
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by