using loops and arrays to solve for variables in equations

Hi I am new to matlab and am trying to become more familiar with using arrays and loops to solve for variables in equations. The goal of this script is to create an array of values for the variables 'x' and 'vis' (viscosity). I know I am not doing somethings quite right. The end result in my workspace are 0x0 matrices for epsUse, x, and vis. The loop I created for temperature (Texplore/Tuse) only seems to use one value instead of all 5. Early in the script I designate epsUse=1e-14 and later on I set the %flow law equation=epsUse to attempt to solve for the variable x. Any tips/direction/help would be appreciated!
clc
clear
close all
x=zeros(5:1);
vis=zeros(5:1);
%strain rate
epsUse=1e-14;
Puse=1e9;
PARAMS.R=8.314; %ideal gas constant
%dry anorthite flow law parameters
n=3;
A=5e12;
Q=641000;
V=24e-6;
%temperature array
Texplore=[200 400 600 800 1000];
for i=1:length(Texplore)
Tuse=Texplore(i);
%fugacity
fugacity=5521*exp((-31.28*1e3+Puse*20.09e-6)/(PARAMS.R*Tuse));
%correction factor
correctionFACTOR=(3^(n+1))/2;
%flowlaw
epsUse=correctionFACTOR*A*fugacity*x^n*exp((-Q+Puse*V)/(PARAMS.R*Tuse));
%viscosity
vis=x/(2*epsUse);
end

1 Kommentar

Torsten gave you the corrected code. For an explanation, when you use : to create the size vector for a call to zeros, ones, etc. and you didn't intend to do that, you either get an empty array:
sz = 5:1
sz = 1x0 empty double row vector
x = zeros(sz)
x = []
whatYouProbablyWanted = zeros(5, 1)
whatYouProbablyWanted = 5×1
0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
or you may get something much larger than you expected (or an error.)
whatYouWanted = zeros(1,20) % works
whatYouWanted = 1×20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = zeros(1:20) % does not work
Error using zeros
Requested array exceeds the maximum possible variable size.
That latter line of code and the error tells you you cannot make a 1-by-2-by-3-by-4-by-5-by-6-by-7-by-8-by-9-by-10-by-11-by-12-by-13-by-14-by-15-by-16-by-17-by-18-by-19-by-20 array.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Torsten
Torsten am 20 Sep. 2024
You mean this ?
x=zeros(5,1);
vis=zeros(5,1);
%strain rate
epsUse=1e-14;
Puse=1e9;
PARAMS.R=8.314; %ideal gas constant
%dry anorthite flow law parameters
n=3;
A=5e12;
Q=641000;
V=24e-6;
%temperature array
Texplore=[200 400 600 800 1000];
for i=1:length(Texplore)
Tuse=Texplore(i);
%fugacity
fugacity=5521*exp((-31.28*1e3+Puse*20.09e-6)/(PARAMS.R*Tuse));
%correction factor
correctionFACTOR=(3^(n+1))/2;
%flowlaw
x(i) = (epsUse/(correctionFACTOR*A*fugacity*exp((-Q+Puse*V)/(PARAMS.R*Tuse))))^(1/n);
%epsUse=correctionFACTOR*A*fugacity*x^n*exp((-Q+Puse*V)/(PARAMS.R*Tuse));
%viscosity
vis(i)=x(i)/(2*epsUse);
end

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023a

Tags

Gefragt:

am 20 Sep. 2024

Kommentiert:

am 20 Sep. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by