help with nested loops

9 ビュー (過去 30 日間)
Corey
Corey 2025 年 3 月 13 日
編集済み: Voss 2025 年 5 月 9 日
Hi all, I know I am not quite getting this right but I am trying to run a nested loop that will output an array of temperatures for every eps value. If I fix the eps value the script will run fine over the range of temperatures but I would like to save time and be able to vary temperature and eps. Any help would be greatly appreciated. Do I need to pre allocate zero arrays with as many rows as there are eps values?
%range of temperatures
Texplore=[0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1400 1600 1800 2000];
epsexplore=[1e-7 1e-8 1e-9 1e-10 1e-11];
%pre allocate stress array
pl=zeros(17,1);
qz=zeros(17,1);
plD=zeros(17,1);
%solve for stress
for i=1:length(Texplore)
Tuse=Texplore(i);
for j=length(epsexplore)
epsuse=epsexplore(j);
pl(i,j)=((epsuse)/(A*exp(-Q/(R*Tuse))))^(1/n);
qz(i,j)=((epsuse)/(A1*exp(-Q1/(R*Tuse))))^(1/n1);
plD(i,j)=((epsuse)/(A2*exp(-Q2/(R*Tuse))))^(1/n);
end
end
  1 件のコメント
Voss
Voss 2025 年 3 月 13 日
編集済み: Voss 2025 年 5 月 9 日
"If I fix the eps value the script will run fine over the range of temperatures but I would like to save time and be able to vary temperature and eps."
Here, j takes one value, which is length(epsexplore):
for j=length(epsexplore)
To have j span the length of epsexplore would be:
for j=1:length(epsexplore)

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2025 年 3 月 13 日
You don’t actually need nested loops (or for that matter, any loops) to calculate those in MATLAB.
Texplore=[0 100 200 300 400 500 600 700 800 900 1000 1100 1200 1400 1600 1800 2000];
epsexplore=[1e-7 1e-8 1e-9 1e-10 1e-11];
[Tuse,epsuse] = ndgrid(Texplore, epsexplore);
A = rand
A = 0.4526
A1 = rand
A1 = 0.0116
A2 = rand
A2 = 0.8720
Q = rand
Q = 0.9927
Q1 = rand
Q1 = 0.8255
Q2 = rand
Q2 = 0.7760
R = rand
R = 0.7689
n = randi(10)
n = 10
n1 = randi(10)
n1 = 2
pl = ((epsuse)./(A*exp(-Q./(R*Tuse)))).^(1/n);
qz = ((epsuse)./(A1*exp(-Q1./(R*Tuse)))).^(1/n1);
plD = ((epsuse)./(A2*exp(-Q2./(R*Tuse)))).^(1/n);
figure
surf(Tuse,epsuse,pl)
grid on
colormap(turbo)
figure
surf(Tuse,epsuse,qz)
grid on
colormap(turbo)
figure
surf(Tuse,epsuse,plD)
grid on
colormap(turbo)
This aproach uses vectorization and element-wise division and exponentiation.
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by