How to multiply, in a loop, a function by a variable from a matrix and plot the change.

5 views (last 30 days)
William on 29 Jan 2023
In a loop the variable 'x' is changing each itteration and I plot that. I want to include in this plot 'p3' as defined in line 18 of the code.
The ploted line of p3 suggests the value of 'x3' is a consistant 1; when I know that 'x' (which is [x1;x2;x3]) is changing with each itteration.
--Code used--
x1=1;
x2=1;
x3=1;
for j=300:20:2000
h3=j;
x=[x1;x2;x3];
c=[500;100;2000;];
A=[-1,-1,-1;1,0,0;0,1,0;0,0,1;200,2300,h3];
b=[-150;25;120;150;180000];
x=linprog(c,A,b);
A;
x;
p3=(95-(j-700)*.1)*x3;
plot(j,x,'.',j,p3,'.'); hold on
end
Note: "linprog" is for system optimization and is minimizing c^T*x subject to A*x<=b
the negative straight line is "p3" and only looks like this because the variable 'x3' is not updating (I think)
Also I am getting this error code in the terinal window (probable related)
Error using plot
Vectors must be the same length.
Error in Loop_Maximization_h3 (line 20)
plot(j,x,'.',j,p3,'.'); hold on
(thanks in advanse everyone, Started MATLAB this week)

Sulaymon Eshkabilov on 29 Jan 2023
Here is the corrected code with some modifications to imptove the computational efficiency of the code. Note that x1 is not changing over the simulation time.
ii=0;
for j=300:20:2000
h3=j;
c=[500;100;2000;];
A=[-1,-1,-1;1,0,0;0,1,0;0,0,1;200,2300,h3];
b=[-150;25;120;150;180000];
ii=ii+1;
x=linprog(c,A,b);
X{ii}=x;
if ~isempty(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
p3=(95-(j-700)*.1)*x3;
PP3(ii)=p3;
else
fprintf('Optimal Solution Found after %d iterations \n', ii-1)
break
end
end
Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. Optimal solution found. No feasible solution found. Linprog stopped because no point satisfies the constraints.
Optimal Solution Found after 63 iterations
XX = reshape(vertcat(X{:}), 3,numel(X)-1);
j=300:20:2000;
yyaxis left
plot(j(1:ii-1), XX(1,:), 'd','markerfacecolor', 'y', 'DisplayName','x_1')
hold on
plot(j(1:ii-1), XX(2,:), 'o', 'markerfacecolor', [0.75 .75 .75], 'DisplayName','x_2')
plot(j(1:ii-1), XX(3,:), '<', 'MarkerFaceColor',[.55 .85 .55], 'DisplayName', 'x_3')
yyaxis right
plot(j(1:ii-1), PP3, 'rs', 'markerfacecolor', 'c', 'DisplayName','p_3')
legend('show', 'location', 'SW')
grid on
Sulaymon Eshkabilov on 29 Jan 2023
All these changes are introduced to make the code more efficient and save solutions (X{ii}, PP3{ii} in cell array) from each iteration. Plotting the computed data outside of the loop is much more efficient.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by