Hey everyone,
I have many variables in my code that increase their size with a for bucle, and I've been realized of the code assistance adverticement. I read the advice and try to apply it, but I couldn't solve it. I am trying to solve ir because it's going to be and extens code and I'm try to not expend many resources in that kind of things. (I know that my code isn't optimum, jaja) but that's really bugging me.
Thanks for your help and have a great day.
P.S: This is the suggestion "Variable 'name of the variable' appers to change size on every loop iteration"
Dhx=[];
Dgx=[];
Dfx=[];
for s=x(1:end)
gh= diff(hx, (s));
gg= diff(gx, (s));
gf= diff(func, (s));
Dfx=[Dfx, gf];
Dhx=[Dhx, gh];
Dgx=[Dgx, gg];
end

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 17 Mai 2020
Bearbeitet: Ameer Hamza am 17 Mai 2020

1 Stimme

Try changing to something like this
Dhx=cell(1,numel(x));
Dgx=cell(1,numel(x));
Dfx=cell(1,numel(x));
for i=1:numel(x)
Dhx{i} = diff(hx, x(i));
Dgx{i} = diff(gx, x(i));
Dfx{i} = diff(func, x(i));
end
Dhx = [Dhx{:}];
Dgx = [Dhx{:}];
Dfx = [Dhx{:}];

9 Kommentare

for i = 1 : numel(s)
Dhx{i} = diff(hx, x(i));
Dgx{i} = diff(gx, x(i));
Dfx{i} = diff(func, x(i));
end
Ameer Hamza
Ameer Hamza am 17 Mai 2020
Thanks for drawing attention to this oversight.
Abner Ojeda
Abner Ojeda am 17 Mai 2020
Hey guys,
Thanks for your responses. It didn't work, I tried with your advices but I got nothing. The code works, and still do what it has to do but the message still there.
Thanks for your help, I really appreciate it!
Greetings
Ameer Hamza
Ameer Hamza am 17 Mai 2020
What is the error?
Abner Ojeda
Abner Ojeda am 17 Mai 2020
Hey Ameer,
There's not an error, is more like an advice and is this:
"Variable 'name of the variable' appers to change size on every loop iteration"
Change 'name of the variable' for Dhx, Dgx, Dfx. I tried to apply the recomendations like create and define prevously a huge array, to not expend many rasources in create in each iteration a new array with new dimenssions but surely I didn't know how to implement it.
Thank you!
Ameer Hamza
Ameer Hamza am 17 Mai 2020
Can you show your code?
"Variable 'name of the variable' appers to change size on every loop iteration"
That is not an error, that is a warning.... and it should not occur if you are using Ameer's code.
nx = numel(x);
Dhx=cell(1,nx);
Dgx=cell(1,nx);
Dfx=cell(1,nx);
for i=1:nx
Dhx{i} = diff(hx, x(i));
Dgx{i} = diff(gx, x(i));
Dfx{i} = diff(func, x(i));
end
Dhx = [Dhx{:}];
Dgx = [Dhx{:}];
Dfx = [Dhx{:}];
Sure, I left you my code below. Ok, Walter, thanks for the answer, maybe I had a mistake copying Ameer's code, I'm going to try again.
Thanks for take your time in answer my question.
P.S: As you can see, I have many variables of this type and that's the most important reason I'm trying to optimize it (at least that part xD)
%%Creación de variables.
clear; close all; clc;
Dhx=[]; Dgx=[]; Dfx=[];
rh=[]; rg=[];
Dhhx=[]; Dggx=[];
KKT=[];
%%Solicitud de función
var= input ('Ingrese el num de var:\n');
syms x [1 var];
func= input ('Ingrese la función la función objetivo: \n');
%%Solicitud de restricciones de igualdad
resi= input ('Ingrese el numero de restricciones \nde igualdad: ');
syms l [1 resi];
for t=1:resi
resth= input(['Ingrese la restricción \nde igualdad ', num2str(t)]);
hx=[rh,resth];
end
%%Solicitud de restricciones de desigualdad
resd=input ('Ingrese el numero de restricciones \nde desigualdad: ');
syms m [1 resd];
for u=1:resd
restg=input(['Ingrese la restricción \nde desigualdad ', num2str(u)]);
gx=[rg, restg];
end
%%Calcúla las derivadas parciales respecto x1, x2...xn y se almacenan.
for s=x(1:end)
gh= diff(hx, (s));
gg= diff(gx, (s));
gf= diff(func, (s));
Dfx=[Dfx, gf];
Dhx=[Dhx, gh];
Dgx=[Dgx, gg];
end
%KKT: Dfx+l·Dhx+m·Dgx=0
for q=1:numel(Dhx)
ff= l.*Dhx(1,q);
Dhhx=[Dhhx, ff];
end
for q=1:numel(Dhx)
ff= m.*Dgx(1,q);
Dhhx=[Dhhx, ff];
end
for p=1:numel(Dfx)
KKT1= (Dfx(1,p)+Dhhx(1,p)+m*Dgx(1,p));
KKT=[KKT, KKT1];
end
Abner Ojeda
Abner Ojeda am 19 Mai 2020
Thanks a lot, guys! I tried Ameer's code and it works, thanks for your help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by