필터 지우기
필터 지우기

For Loop Starting at Zero

조회 수: 14 (최근 30일)
Tony Stianchie
Tony Stianchie 2023년 3월 7일
댓글: dpb 2023년 3월 7일
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

답변 (1개)

dpb
dpb 2023년 3월 7일
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
Tony Stianchie
Tony Stianchie 2023년 3월 7일
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 2023년 3월 7일
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?

댓글을 달려면 로그인하십시오.

카테고리

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