Function optimization with some conditions conditions

6 Ansichten (letzte 30 Tage)
Jon Bilbao
Jon Bilbao am 22 Jun. 2023
Kommentiert: Torsten am 22 Jun. 2023
I want to find the h values that meet the following conditions, h(j)<Hmax, h(j+1)-h(j)<lvasc(j) and h(j+1)-h(j)>lsa(j), the values of lvasc and lsa are calculated in teh function. About the input values ht is a array with a size ht(n,m), Lc and s are scalar and vmax size is vmax (1,m), i have done the function below but it returns me the error:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in hp2 (line 38)
b = [b1;b2;b3];
I dont know how to solve this, thanks in davance
Mathematically, I need to find the values of h that minimize the function I, which is the summation of the subtraction between ht(n,m) and h(m), and h has to satisfy several conditions. The first condition is that h(end) < 2.5 * Lc. The next condition is that h(j+1) - h(j) < (1/36 * 0.5 / vmax(j+1)) or (0.75 * s / 9.81) / (Lc * 2), whichever is more restrictive. Finally, h(j+1) - h(j) > (0.5 * s * 0.4 * (ht(j+1) - ht(j))) / (vmax(j+1) * 9.81).
n = 10;
m = 50;
ht = rand(n,m);
Lc = 2;
s = 15;
vmax = rand(1,m);
[h,fval,exitflag] = hp2(ht,Lc,s,vmax)
ans = 1×2
50 1
ans = 1×2
1 49
ans = 1×2
1 49
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Error in solution>hp2 (line 49)
b = [b1;b2;b3];
function [h,fval,exitflag] = hp2(ht,Lc,s,vmax)
s1=s/1000;
Hmax=2.5*Lc;
Imax=0.75*s1/9.81;
lvasc=1/36*0.5./vmax;
[n,m]=size(ht);
for j=1:m-1
lvasc(j)=1/36*0.5./vmax(j+1);
lv(j)=min(Imax,lvasc(j));
lsa(j) = ((ht(j+1)-ht(j)) * (0.5 * s1 * 0.4)) / (vmax(j+1) * 9.81);
end
I=@(h) sum(sum((ht-h).^2));
h0 = zeros(1,m);
v1 = ones(m,1);
w1 = -ones(m-1,1);
A1 = diag(v1) + diag(w1,1);
b1 = [zeros(m-1,1);Hmax];
v2 = -ones(m,1);
w2 = ones(m-1,1);
A2 = diag(v2) + diag(w2,1);
A2(end,:) = [];
b2 = lv;
v3 = -ones(m-1,1);
w3 = ones(m,1);
A3 = diag(v3,1) + diag(w3);
A3(end,:) = [];
b3 = -lsa;
size(b1)
size(b2)
size(b3)
A = [A1;A2;A3];
b = [b1;b2;b3];
[h,fval,exitflag] = fmincon(I,h0,A,b);
end

Akzeptierte Antwort

Torsten
Torsten am 22 Jun. 2023
Verschoben: Torsten am 22 Jun. 2023
The dimensions would be correct if you use
b = [b1;b2.';b3.'];
instead of
b = [b1;b2;b3];
but you'd better post the problem you are trying to solve in a mathematical form.
  4 Kommentare
Jon Bilbao
Jon Bilbao am 22 Jun. 2023
Bearbeitet: Jon Bilbao am 22 Jun. 2023
Yeah, but in the problem that i am trying to solve thats physically imposible so if it can be made i would like to change it to 0.
Torsten
Torsten am 22 Jun. 2023
function [h,fval,exitflag] = hp2(ht,Lc,s,vmax)
...
[h,fval,exitflag] = fmincon(I,h0,A,b);
h(1) = 0;
end
Now you've set it to 0.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by