For Loop Starting at Zero

12 views (last 30 days)
Tony Stianchie
Tony Stianchie on 7 Mar 2023
Commented: dpb on 7 Mar 2023
I'd like to run my Theta for loop across the length of X ( 0 to 1) with my stored values of BI
H = 0.1;
I = 200;
Y = 0;
BI = zeros(I,1);
b = pi;
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),b);
BI(i,1) = b;
end
BI(end);
X = linspace(0,1,I);
Theta = zeros(I,1);
for k = 1:length(X)
Theta(k) = ((((1.\BI).*(1-cos(BI))).\(0.5-(1.\(4.*BI)))).*sin(BI.*Y).*(cosh(2.*BI.*(k-1))-tanh(2.*BI).*sinh(2.*BI.*(k-1))));
end

Answers (1)

dpb
dpb on 7 Mar 2023
H = 0.1;
I = 200;
Y = 0;
BI = zeros(I,1);
b = pi;
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),b);
BI(i,1) = b;
end
unique(BI)
ans = 3.1731
shows you're starting guess isn't good enough to do more than find the same root over and over and over...try (for a much smaller sample size)
I=5; BI=zeros(I,1);
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),pi*i);
BI(i,1) = b;
end
unique(BI)
ans = 5×1
3.1731 6.2991 9.4354 12.5743 15.7143
diff(BI)
ans = 4×1
3.1260 3.1363 3.1389 3.1400
I don't really know what the last equation is supposed to be, but to evaluate in the loop over k, one presumes you're looking for BI(k) everywhere you have BI and the .\ operators are really supposed to denote division on an element-by-element basis.
Write the equation out longhand so it can be deciphered if you need more help...
  2 Comments
Tony Stianchie
Tony Stianchie on 7 Mar 2023
Thanks. I'd like to Sum (from i =1 to I) at each step along X using my stored values of BI.
So for X = 0, I'd have 5 series summations.
H = 0.1;
I = 5;
Y = 0;
BI = zeros(I,1);
for i = 1:I
b = fzero(@(b)(b*tan(b)-H),pi*i);
BI(i) = b ;
end
X = linspace(0,1,I);
Theta = zeros(I,1);
for k = 1:length(X)
for BI = (I,1)
Theta(k) = ((((1/BI).*(1-cos(BI)))/(0.5-(1/(4.*BI)))).*sin(BI.*Y).*(cosh(2.*BI.*(k-1))-tanh(2.*BI).*sinh(2.*BI.*(k-1))));
end
end
dpb
dpb on 7 Mar 2023
I showed the way to write BI(k) in the loop...oh! I ended up deleting it because it was too hard to try to decipher what was really intended, sorry.
for k = 1:length(X)
Theta(k) = ((((1/BI(k)).*(1-cos(BI(k))))/(....etc., ...
end
Too many parentheses for this old man's eyes to be able to count and the online code editor doesn't automagically match them so leave that to younger eyes than mine... <vbg>
But, there's still confusion about what you really want as there's no X in the equation anywhere; what's the point of it?

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!