Creating a loop inside a script
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Rik
el 17 de Mayo de 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.
Respuestas (1)
Rik
el 17 de Mayo de 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)
6 comentarios
Rik
el 17 de Mayo de 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).
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!