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!