How do I create a loop for this calculation in MATLAB?

1 次查看(过去 30 天)
%I need to create a loop to calculate d in the following manner, any help is greatly appreciated, thank you!
%constants
c = 1.1e-10;
m = 3.3;
p500 = [ -0.3142 2.3172 -7.0249 11.2867];
p2000 = [ -1.2577 9.2728 -28.1056 45.1501];
p3150 = [ -1.9807 14.6042 -44.2653 71.1096];
d = 0.015; % initial value of d
%the following needs to happen 25 times
%the following needs to happen 1 time
dd_dN = c*((0.706*(p3150(1)+3150)+0.537*(2*d/pi)*p3150(2)+0.448*(d^2./2)*p3150(3)+0.393*(4*d^3/(3*pi))*p3150(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
%the following needs to happen 240 times, picking up d from the previous
%calculation
dd_dN = c*((0.706*(p2000(1)+2000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
dd_dN = c*((0.706*(p2000(1)+2000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
dd_dN = c*((0.706*(p2000(1)+2000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
%(three example iterations ^)
%the following needs to happen 1800 times, picking up d from the previous
%calculation
dd_dN = c*((0.706*(p500(1)+500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
dd_dN = c*((0.706*(p500(1)+500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
dd_dN = c*((0.706*(p500(1)+500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
%(three example iterations ^)
%the final output should be d, after (1+240+1800)*25 total calculations

采纳的回答

Torsten
Torsten 2022-5-11
%constants
c = 1.1e-10;
m = 3.3;
p500 = [ -0.3142 2.3172 -7.0249 11.2867];
p2000 = [ -1.2577 9.2728 -28.1056 45.1501];
p3150 = [ -1.9807 14.6042 -44.2653 71.1096];
d = 0.015; % initial value of d
%the following needs to happen 25 times
for i=1:25
%the following needs to happen 1 time
dd_dN = c*((0.706*(p3150(1)+3150)+0.537*(2*d/pi)*p3150(2)+0.448*(d^2./2)*p3150(3)+0.393*(4*d^3/(3*pi))*p3150(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
for j=1:240
%the following needs to happen 240 times, picking up d from the previous
%calculation
dd_dN = c*((0.706*(p2000(1)+2000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
end
%(three example iterations ^)
for j=1:1800
%the following needs to happen 1800 times, picking up d from the previous
%calculation
dd_dN = c*((0.706*(p500(1)+500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN
end
end
%(three example iterations ^)

更多回答(1 个)

Jon
Jon 2022-5-11
Slightly different interpretation of how you want to do loops, I ignored the first comment about 25 iterations. Regardless it seems that you have a problem with convergence as your variables become infinite after a small number of loops. You will need to check more on that.
%constants
c = 1.1e-10;
m = 3.3;
p500 = [ -0.3142 2.3172 -7.0249 11.2867];
p2000 = [ -1.2577 9.2728 -28.1056 45.1501];
p3150 = [ -1.9807 14.6042 -44.2653 71.1096];
d = 0.015; % initial value of d
% % % %the following needs to happen 25 times ** ignore this comment??
%the following needs to happen 1 time
dd_dN = c*((0.706*(p3150(1)+3150)+0.537*(2*d/pi)*p3150(2)+0.448*(d^2./2)*p3150(3)+0.393*(4*d^3/(3*pi))*p3150(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
%the following needs to happen 240 times, picking up d from the previous
%calculation
for k = 1:240
dd_dN = c*((0.706*(p2000(1)+2000)+0.537*(2*d/pi)*p2000(2)+0.448*(d^2./2)*p2000(3)+0.393*(4*d^3/(3*pi))*p2000(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
end
%the following needs to happen 1800 times, picking up d from the previous
%calculation
for k = 1:1800
dd_dN = c*((0.706*(p500(1)+500)+0.537*(2*d/pi)*p500(2)+0.448*(d^2./2)*p500(3)+0.393*(4*d^3/(3*pi))*p500(4))*(sqrt(pi*d)))^m;
d = d + dd_dN;
end
%the final output should be d, after (1+240+1800)*25 total calculations
  1 个评论
Nick Lavanture
Nick Lavanture 2022-5-11
The issue you pointed out was a mistake on my part, the constant I added in those long equations were off by a factor of 1000 (0.5 instead of 500, etcetera.) Thank you for your time

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by