For Loop Starting at Zero
조회 수: 14 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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)
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)
diff(BI)
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!