Creating a loop inside a script

1 view (last 30 days)
Jhon Paul
Jhon Paul on 17 May 2021
Commented: Rik on 17 May 2021
I want to form a loop to avoid the iterations from 1 to 8. The loop is initiated with he value of x4 which is calculated from the value of v. The expression of v includes x6 . The concerned part of the loop is highlighted in red.
clear vars
close all
% Solving Set of Equations %
syms x1 x2 x3 x4 x5 x6 T
eqn = [
200*x1-x2-70600 ==0
x5+0.25*x2*x3-x6==0
x2*x3-1E-4*x6^2==0
x6+0.12*x2*x4-1373==0]
sol = solve(eqn, [x1, x2, x3, x5]);
SOL = simplify([sol.x1; sol.x2; sol.x3; sol.x5])
% Extracting best solution of x6 interms of x4
residue = sum((SOL - [603; 5*10^4; 3.85*10-3; 1324]).^2)
sympref('TypesetOutput',false);
bestx6 = solve(diff(residue, x6),x6, 'maxdegree', 3)
x4vals = linspace(1e-8, 17E-3, 100);
X6 = double(subs(bestx6(1:6), x4, x4vals)).'; %should be 100 x 2
subplot(3,1,1); plot(x4vals, real(X6(:,1:2))); title('real'); legend({'Root 1', 'Root 2',});'Linewidth',5;
subplot(3,1,2); plot(x4vals, real(X6(:,3:4))); title('real'); legend({'Root 3', 'Root 4',});'Linewidth',5;
subplot(3,1,3); plot(x4vals, real(X6(:,5:6))); title('real'); legend({'Root 5', 'Root 6',});'Linewidth',5;
% Iteration 1( t=900 secs):
% Solving x4 for a duration of 900 secs
% x4 is incremented with respect to velocity variable v
% v is calculated as v= 1.14 * 1E-12 * x6 ^2*exp (-29*(1373/x6)-1)
% for the the first iteration x6= 1373 implying v = 2.2*10^-6
t = 900;
x0 = [603, 5*(10^4), 3.85*10-3, 0, 1324, 1373];
x41 = vpa(x0(4) + 2.2*10^-6 * t)
%Solving x6 for a duration of 900 secs
x61=double(real(vpa(subs(bestx6(1),x41))))
% % x62= real(vpa(subs(bestx6(2),x41)))
% % x63= real(vpa(subs(bestx6(3),x41)))
% % x64= real(vpa(subs(bestx6(4),x41)))
% Solving x2 for a duration of 900 secs
% x6+0.12*x2*x4-1373==0 %
x21=double((1373-x61)/(0.12*x41))
% % Solving x3 for a duration of 900 secs
% % x2*x3-1E-4*x6^2==0
x31 =double((1E-4*x61^2)/x21)
% Solving x1 for a duration of 900 secs
% %Equation 200*x1-x2-70600 ==0
x11 = double((70600+x21)/200)
% Solving x5 for a duration of 900 secs
% Equation x5+0.25*x2*x3-x6==0
x51=double(x61-0.25*x21*x31)
% Iteration 2( t=1800 secs):
% Solving x4 for a duration of 1800 secs
t = 1800;
x42 = double(x41 + 1.64*10^-6*t)
% Solving x6 for a duration of 1800 secs
x62=double(real(vpa(subs(bestx6(1),x42))))
% Solving x2 for a duration of 1800 secs
% x6+0.12*x2*x4-1373==0 %
x22=double((1373-x62)/(0.12*x42))
% Solving x3 for a duration of 1800 secs
% x2*x3-1E-4*x6^2==0
x32 =double((1E-4*x62^2)/x22)
% Solving x1 for a duration of 1800 secs
% Equation 200*x1-x2-70600 ==0
x12 = double((70600+x22)/200)
% Solving x5 for a duration of 1800 secs
% Equation x5+0.25*x2*x3-x6==0
x52=double(x62-0.25*x22*x32)
  2 Comments
Rik
Rik on 17 May 2021
I don't see anything in red. I do see a lot of numbered variables, which are a sign of storing data in variables names, instead of in variables. It is not clear to me what the problem is you want help with, especially as your code runs without error.
Have a read here and here. It will greatly improve your chances of getting an answer.
Jhon Paul
Jhon Paul on 17 May 2021
You see in the code there is two Iteration 1 & 2. For simplicity I have kept 2, but infact there will be much more. So I want to make it through a loop in place of doing the calculation inside the loop every time . For that the following information shall be considered as you can see in the code:
the loop starts with calculating x4. Then x4 is incremented with respect to velocity variable v. The variable v is claculated with respect to x6 given by:
v= 1.14 * 1E-12 * x6 ^2*exp (-29*(1373/x6)-1)
The intial condition shall be used to initialise the loop as given below :
x0 = [603, 5*(10^4), 3.85*10-3, 0, 1324, 1373];
Hope my querry is now clear . Any help will be appreciated.

Sign in to comment.

Answers (1)

Rik
Rik on 17 May 2021
It sounds like your loop needs to have a shape like this:
clear
syms x1 x2 x3 x4 x5 x6 T
eqn = [
200*x1-x2-70600 ==0
x5+0.25*x2*x3-x6==0
x2*x3-1E-4*x6^2==0
x6+0.12*x2*x4-1373==0];
sol = solve(eqn, [x1, x2, x3, x5]);
SOL = simplify([sol.x1; sol.x2; sol.x3; sol.x5]);
% Extracting best solution of x6 interms of x4
residue = sum((SOL - [603; 5*10^4; 3.85*10-3; 1324]).^2);
sympref('TypesetOutput',false);
bestx6 = solve(diff(residue, x6),x6, 'maxdegree', 3);
x4vals = linspace(1e-8, 17E-3, 100);
X6 = double(subs(bestx6(1:6), x4, x4vals)).'; %should be 100 x 2
%skip the plotting for this example
% subplot(3,1,1); plot(x4vals, real(X6(:,1:2))); title('real'); legend({'Root 1', 'Root 2',});
% subplot(3,1,2); plot(x4vals, real(X6(:,3:4))); title('real'); legend({'Root 3', 'Root 4',});
% subplot(3,1,3); plot(x4vals, real(X6(:,5:6))); title('real'); legend({'Root 5', 'Root 6',});
t_vec = [900 1800];
x51_results=NaN(size(t_vec));
for it=1:numel(t_vec)
t=t_vec(it);
x0 = [603, 5*(10^4), 3.85*10-3, 0, 1324, 1373];
x41 = vpa(x0(4) + 2.2*10^-6 * t);
%Solving x6
x61=double(real(vpa(subs(bestx6(1),x41))));
% Solving x2
% x6+0.12*x2*x4-1373==0 %
x21=double((1373-x61)/(0.12*x41));
% Solving x3
% x2*x3-1E-4*x6^2==0
x31 =double((1E-4*x61^2)/x21);
% Solving x1
%Equation 200*x1-x2-70600 ==0
x11 = double((70600+x21)/200);
% Equation x5+0.25*x2*x3-x6==0
x51=double(x61-0.25*x21*x31);
% Here you can store the results you want to keep in an index array,
% e.g. a cell array.
x51_results(it)=x51;
end
disp(x51_results)
1.0e+03 * 1.3148 1.3037
  6 Comments
Jhon Paul
Jhon Paul on 17 May 2021
Sorry my coding skill is too poor and I havent got that much time now . if u can help.
Rik
Rik on 17 May 2021
I already showed you the disp function and how you can index a vector to store a result at the end of your loop. How did you try to adapt this?
Matlab is not like MS Paint. You can't just install it and expect to master it after 2 minutes. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
If a problem is too large to solve, try breaking it up in smaller parts. What you need to do is to figure out which values you want to store. Then you can put them in arrays, just like I did with x51. Using disp (either in the loop or outside it) you can display the values to the user. If you want more control over the format, you can use fprintf (read the documentation for examples and an explanation).

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!