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

1 visualización (últimos 30 días)
Nick Lavanture
Nick Lavanture el 11 de Mayo de 2022
Comentada: Nick Lavanture el 11 de Mayo de 2022
%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

Respuesta aceptada

Torsten
Torsten el 11 de Mayo de 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 ^)

Más respuestas (1)

Jon
Jon el 11 de Mayo de 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 comentario
Nick Lavanture
Nick Lavanture el 11 de Mayo de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by