How to make variable names using for loop

Answers (1)

for k=1:50
v(k)=0;
end

10 Comments

Isn't this answer using a vector? I need to define 50 variables each by each. I cannot use matrix or vector for some reason.
I am making a code something like this.
function [xx,exitflag] = VL_LP_values
a1=9.9872;
a2=9.6974;
R=-57348.67749;
lambda1=0.1300;
lambda2=0.0774;
z1=10.0038;
z2=10.0056;
beta=0.97
optimoptions('fsolve')
options = optimoptions('fsolve');
options.Algorithm = 'trust-region-dogleg'
%options.Algorithm = 'Levenberg-Marquardt'
options.MaxFunctionEvaluations = 10000
options.MaxIterations = 10000
options.StepTolerance = 1e-10
options.FunctionTolerance = 1e-10
options.OptimalityTolerance: 1e-10
for k=1:50
vl(k)=0;
end
x0=[vl1, vl2, vl3, vl4, vl5, vl6, vl7, vl8, vl9, vl10, vl11, vl12, vl13, vl14, vl15, vl16, vl17, vl18, vl19, vl20, vl21, vl22, vl23, vl24, vl25, vl26, vl27, vl28, vl29, vl30, vl31, vl32, vl33, vl34, vl35, vl36, vl37, vl38, vl39, vl40, vl41, vl42, vl43, vl44, vl45, vl46, vl47, vl48, vl49, vl50];
[x0 fval exitflag]=fsolve(@VL_LP_equations,x0,options,v1,v2,u1,u2,w1,w2,a1,a2,R,LL,deltaL,lambda1,lambda2,z1,z2,beta)
for k=1:50
vl(k)=x0('k');
end
end
Oh, okay. Please try the code below.
clear;
for k=1:50
eval(['v',num2str(k),'=0']);
end
v1 = 0
v2 = 0
v3 = 0
v4 = 0
v5 = 0
v6 = 0
v7 = 0
v8 = 0
v9 = 0
v10 = 0
v11 = 0
v12 = 0
v13 = 0
v14 = 0
v15 = 0
v16 = 0
v17 = 0
v18 = 0
v19 = 0
v20 = 0
v21 = 0
v22 = 0
v23 = 0
v24 = 0
v25 = 0
v26 = 0
v27 = 0
v28 = 0
v29 = 0
v30 = 0
v31 = 0
v32 = 0
v33 = 0
v34 = 0
v35 = 0
v36 = 0
v37 = 0
v38 = 0
v39 = 0
v40 = 0
v41 = 0
v42 = 0
v43 = 0
v44 = 0
v45 = 0
v46 = 0
v47 = 0
v48 = 0
v49 = 0
v50 = 0
@Angelo Yeo Thanks a lot! It solved the problem. However, I encountered another problem below...
function F = VL_LP_equations(x0,v1,v2,u1,u2,w1,w2,a1,a2,R,LL,deltaL,lambda1,lambda2,z1,z2,beta)
for k=1:50
eval(['vl',num2str(k),'=x0','(',num2str(k),')']);
end
for k=1:50
h1('k')=(a1*((v1('k')+eval(['vl',num2str(k)])*(R('k')-deltaL('k'))))/(u1('k')+(v1('k')+eval(['vl',num2str(k)])*(R('k')-deltaL('k')))))
h2('k')=(a2*((v2('k')+eval(['vl',num2str(k)])*(-R('k'))))/(u2('k')+(v2('k')+eval(['vl',num2str(k)])*(-R('k')))))
manufacturing('k')=h1('k')*w1('k')+(1-h1('k'))*z1+beta*h1('k')*((1-f1)*w1('k')+f1*z1)+beta*(1-h1('k'))*(h1('k')*w1('k')+(1-h1('k'))*z1)
service('k') =h2('k')*w2('k')+(1-h2('k'))*z2+beta*h2('k')*((1-f2)*w2('k')+f2*z2)+beta*(1-h2('k'))*(h2('k')*w2('k')+(1-h2('k'))*z2)
end
How can I make inside this for loop vl1, vl2, vl3, vl4 .... etc?
"I need to define 50 variables each by each. I cannot use matrix or vector for some reason. "
What is that "some reason" ?
@Stephen23 I am using fsolve, which needs x0, a vector. It is hard to explain because I am noob. Let me share a link that I am working on right now.
Stephen23
Stephen23 on 4 Oct 2022
Edited: Stephen23 on 4 Oct 2022
"How can I make inside this for loop vl1, vl2, vl3, vl4 .... etc?"
Ugh, do NOT do this: your bad data design forces you into writing bad code. The answer to your question is that you would have to write more slow, complex, inefficient, buggy code that uses EVAL for every single line of code.
"However, I encountered another problem below..."
The basic problem is actually that you are forcing meta-data (i.e. pseudo-indices) into the variable names. Instead of doing that, you should use arrays, just like MATLAB is designed for. Once you learn to use arrays, then your task will be simpler and much more efficient. Your current approach should be avoided.
" It is hard to explain because I am noob."
Then it is extremely unlikely that "I need to define 50 variables each by each. I cannot use matrix or vector for some reason", and this is much more likely:
@Stephen23 Thanks a lot... I dont have time right now because I need to finish it by tonight. But I will learn array.
"I dont have time right now because I need to finish it by tonight."
The inefficiency of your approach is not just runtime, but also programming and debugging time.
You want to define and initialize 50 individual variables, and then you want to put the values of the variables together in a vector. You then overwrite the content of the individual variables with the results of the call.
In your perception, how does that differ from starting with just creating the vector (without putting the values into individual variables), passing the vector to the function, and then having code that splits the function up into the variables? Considering that you overwrite the variables after the call, what is going on in the code that requires that the initial values are written into individual variables before being put into the vector?
When you have code like
A=0; B=0;
C = [A, B];
then at the time the [A, B] is executed, the current values associated with A and B are extracted and put together into the list, and the resulting list is just of values. MATLAB does not keep any memory of where the values came from. If you were to examine C afterwards, you would be completely unable to determine that the first element was copied from A and the second from B. C=[A, B] is not creating a formula. If you were to alter A to 5 afterwards, C would not suddenly start evaluating to [5,0].
So inside the function that is receiving x0, there is no way to examine (for example) the 19th element of the vector and query "Which variable name did this element come from?"
An expression such as C=[A, B] is not creating a list of addresses of A and B. You cannot, for example, say "write 7 into the variable that was used for the second element of C" and then expect that B will now have the value 7. Only the values associated with the variables make it into C, with absolutely no "history" kept of where the values came from.
And that means that you might as well just use x0=zeros(1,50); without writing the values into individual variables first.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 4 Oct 2022

Commented:

on 4 Oct 2022

Community Treasure Hunt

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

Start Hunting!