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

3 views (last 30 days)
%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

Torsten
Torsten on 11 May 2022
%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 ^)

More Answers (1)

Jon
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
Nick Lavanture on 11 May 2022
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!