How do I create a loop for this calculation in MATLAB?
Show older comments
%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
Accepted Answer
More Answers (1)
Jon
on 11 May 2022
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 Comment
Nick Lavanture
on 11 May 2022
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!