Solving equations in a for loop with changing variables

4 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by