using loops and arrays to solve for variables in equations
2 views (last 30 days)
Show older comments
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 Comment
Steven Lord
on 20 Sep 2024
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
x = zeros(sz)
whatYouProbablyWanted = zeros(5, 1)
or you may get something much larger than you expected (or an error.)
whatYouWanted = zeros(1,20) % works
y = zeros(1:20) % does not work
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.
Accepted Answer
Torsten
on 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
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!