How to perform iterations for lopp

2 views (last 30 days)
sunitha
sunitha on 10 Feb 2021
Commented: Walter Roberson on 11 Mar 2021
I have imported N and V values from excel, only these two variables are varying. Now i have to write the code for Qo=1000,i have to find N & J values then Qo=2500 find N& J values upto 10000.For N & V i gave 23 inputs .so for Qo=1000 i have to get 23 different N & J values, but here iam getting the values for only one Qo value
Qo=1000:1500:10000;
nX =length(No);
q = length(Qo);
q1 = q*nX;
Solutions= cell(q1, 2);
for i=1:q
for S=1:nX
X =((bt*Qo(i)*(1+R)*Xo)+(Y*1000*a*V(S)*J*bs))/(bt*((Qo(i)*(1+R))-((V(S)*Y*k*N)/((Kn*1000)+N))+(V(S)*kd)));
eqn1=(Qo(i)*(1+R)*(No(S)-N)-(V(S)*((a*J*1000)+((X*k*N)/((Kn*1000)+N)))))==0;
eqn2=(sqrt((Lstar^1.76)+(5.2*(Nstar-Nstar_min)))-(Lstar^0.88))/2.6==jstar^0.88;
Solutions{S} = vpasolve([eqn1,eqn2],[N,J]);
end
  2 Comments
Walter Roberson
Walter Roberson on 10 Feb 2021
you have a double loop but your output is only indexed by one of the two variables
sunitha
sunitha on 10 Feb 2021
U r right..in my code only last value of 'Qo' that is 10,000 is considered for the calculation. Can u adjust the code so that I get iterations for all Qo values

Sign in to comment.

Answers (5)

Walter Roberson
Walter Roberson on 11 Feb 2021
Edited: Walter Roberson on 11 Feb 2021
syms N J
Qo=1000:1500:10000;
nX =length(No);
q = length(Qo);
Solutions= cell(nX, q);
for i=1:q
for S=1:nX
X =((bt*Qo(i)*(1+R)*Xo)+(Y*1000*a*V(S)*J*bs))/(bt*((Qo(i)*(1+R))-((V(S)*Y*k*N)/((Kn*1000)+N))+(V(S)*kd)));
eqn1=(Qo(i)*(1+R)*(No(S)-N)-(V(S)*((a*J*1000)+((X*k*N)/((Kn*1000)+N)))))==0;
eqn2=(sqrt((Lstar^1.76)+(5.2*(Nstar-Nstar_min)))-(Lstar^0.88))/2.6==jstar^0.88;
Solutions{S, i} = vpasolve([eqn1,eqn2],[N,J]);
end
end

sunitha
sunitha on 16 Feb 2021
Edited: Walter Roberson on 16 Feb 2021
Hello sir,
I am facing the problem in this code.At first it will take No value =400 it will go to loop perform iterations and after again it has to take 500 value. i am getting zero values for second No values.
No=400:100:600; n=3; N=4;
aX=length(No);
A=cell(1,aX);
for i=1:aX
for j=1:1:3 %%%%% no of reactors
Ne(j,i)=No(j,i)*(N/No(j,i))^(j./n);
No(j+1,i+1)=Ne(j,i);
end
end ;
  3 Comments
sunitha
sunitha on 17 Feb 2021
I am getting the error that index in position 1 exceeds array and I am getting only one value
Walter Roberson
Walter Roberson on 17 Feb 2021
At what point in your code do you write to location No(2,1) ?

Sign in to comment.


sunitha
sunitha on 17 Feb 2021
fFOR
I have to do 3 iterations,I st iteration it has to take No=400,calculate J1 value and goes to 2nd iteration
for i=1:aX
N1=No(i);
for j=1:3 %%%%% no of reactors
Ne(i,j)=N1(j)*(N/N1(j))^(j./n);
N1(j+1)=Ne(i,j);
Ans(i,1)=No(i);
Ans(i,j+1)=Ne(i,j);
eqn1=(sqrt((Lstar^1.76)+(5.2*(Nstar-Nstar_min)))-(Lstar^0.88))/2.6==jstar^0.88;
soln=vpasolve([eqn1],[J]);
J1=double(soln);
end
end
  1 Comment
Walter Roberson
Walter Roberson on 17 Feb 2021
J1(i,j) = double(soln);
However, notice that
eqn1=(sqrt((Lstar^1.76)+(5.2*(Nstar-Nstar_min)))-(Lstar^0.88))/2.6==jstar^0.88;
does not use Ne or N1 or No or Ans (the variables you assign to in the loops) so you are going to be calculating exactly the same thing every iteration.
Your original code with two equations in N and J had the same problem. Also, there was no obvious connection between the variables such as Lstar and the variables N or J, so the fragments posted before give the impression that the equation is independent of the variable, J, being solved for.

Sign in to comment.


sunitha
sunitha on 17 Feb 2021
Edited: sunitha on 17 Feb 2021
i am using calculated N value in finding J variable and V value . Nstar is a function of N. Here i have a doubt i want to give No values intitial No ,after every iteration whatever i got the value that has to be taken into No. How can i No values here in finding V??
for i=1:n
Ne(j,i)=N1(i)*(N/N1(i))^(i./n);
Se(j,i)=transpose(Ne(j,i));
N1(i+1)=Ne(j,i);
Nstar(j,i)=Se(j,i)/(1000*Kn)
jstar=((sqrt((Lstar^1.76)+(5.2*(Nstar(j,i)-Nstar_min)))-(Lstar^0.88))/2.6)^1.136;
J(j,i)=jstar*(sqrt(Kn*k*Xf*Df));
Ans(j,1)=No(j);
Ans(j,i+1)=Ne(j,i);
J1(j,i)=transpose(J(j,i));
So=transpose(No); %%This is the input values in finding the V value
A=(a*J1(j,i)*1000)*((bt*kd*((Kn*1000)+Se(j,i)))-(Y*k*Se(j,i)*kd));
B=bt*Qo*(1+R)*((k*Se(j,i))*(Y*(No-Se(j,i))+ Xo)+(((Kn*1000)+Se(i,j))*((a*J1(j,i)*1000)-(kd*(No(i,j)-Se(i,j))))));
C=(Qo^2)*((1+R)^2)*(Se(i,j)-No(i,j))*((Kn*1000)+Se(i,j))*bt;
V(j,i)=(sqrt((B.^2)-(4*A*C))-B)/(2*A);
end
end
  5 Comments
sunitha
sunitha on 17 Feb 2021
I can change the No to So in the equation. How can I define the No value in the equation
Walter Roberson
Walter Roberson on 17 Feb 2021
Ans(j,1)=No(j);
That tells us No is a vector. It was a vector before in previous posts but you changed your code and did not show assigning to it, so we could not assume that it is still a vector in your current code.
B=bt*Qo*(1+R)*((k*Se(j,i))*(Y*(No-Se(j,i))+ Xo)+(((Kn*1000)+Se(i,j))*((a*J1(j,i)*1000)-(kd*(No(i,j)-Se(i,j))))));
The first access to No is not indexed so it uses the whole vector and so B will be a vector of results.
The second reference to No is indexed with two variables, implying that No is two dimensional instead of a vector, which is likely a problem compared to using it as a vector for Ans.
V(j,i)=(sqrt((B.^2)-(4*A*C))-B)/(2*A);
B is a vector or possibly an array so the right hand side is vector or array but you are storing it in a scalar location.

Sign in to comment.


sunitha
sunitha on 11 Mar 2021
Hello sir,
when running the program i am getting the error and i couldnt understand the error .can u please tell how to rectify this error??
  1 Comment
Walter Roberson
Walter Roberson on 11 Mar 2021
eqn1=(jstar*Lstar)+((0.5*jstar^2)+(jstar((1+((jstar/3.4)^1.19)^-0.61))))/tanh(jstar/Nstar_min)==Nstar;
^^^^^^
In MATLAB, NAME followed by ( can mean one of several things:
  1. NAME is a variable that has been assigned a function handle, and the function is to be invoked passing in whatever list is inside the (); or
  2. NAME is not a variable, but is the name of a function that is "visible" in the scope, and the function is to be invoked passing in whatever list is inside the (); or
  3. NAME is not a variable, but is the name of a method for the dominent object class encountered inside the () and the class method is to be called for the object class; or
  4. NAME is not a variable, but is the name of a function that the user expects to be in scope, but is not, and the call is in error
  5. NAME is a variable and what follows inside the () is a list of indices that are comprised of positive integers, or logical values, or character vectors (but not string objects), which are to act to index the variable; or
  6. NAME is a variable and what follows in the () includes things that are not positive integers or logical values or character vectors, and the call is an invalid indexing attempt
Your jstor is not the name of a function or a variable designating a function handle: it is the name of a scalar expression, so jstor can only be followed immediately by () for indexing. However, (1+((jstar/3.4)^1.19)^-0.61) is not a positive integer, so the indexing attempt fails.
There are absolutely no circumstances in MATLAB in which NAME followed immediately by () is ever treated as implied multiplication. Never. If you intend multiplication you must always use .* (element-by-element multiplication) or * (algebraic matrix multiplication -- inner product)

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!