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
댓글 수: 0
채택된 답변
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
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
참고 항목
카테고리
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!