Solving equations in a for loop with changing variables

1 vue (au cours des 30 derniers jours)
Samantha Andeson
Samantha Andeson le 30 Oct 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 commentaires
Stephan
Stephan le 30 Oct 2020
please insert your code - we can not copy a code from a picture...
Samantha Andeson
Samantha Andeson le 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

Connectez-vous pour commenter.

Réponses (1)

Juan Sagarduy
Juan Sagarduy le 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by