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

3 Ansichten (letzte 30 Tage)
William am 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)
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Sulaymon Eshkabilov am 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
##### 2 KommentareKeine anzeigenKeine ausblenden
William am 29 Jan. 2023
Thankyou, referancing the variable again in the loop "x3=x(3)" and refering it back to the matrix was what i needed.
I really appreciate the better formatted graph, seeing the yyaxis left/right was very nice, and spliting up the plotted "x" into three sections so they can have their own symbol and collor was interesting.
I dont understand what the if/else section was doing, or the "X{ii}=x", or the PP3(ii)=p3. Aslo, it lookes like your plot was outside the loop and written after the loop was executated, how did you do that?
Sulaymon Eshkabilov am 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.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by