Function optimization with some conditions conditions

3 ビュー (過去 30 日間)
Jon Bilbao
Jon Bilbao 2023 年 6 月 22 日
コメント済み: Torsten 2023 年 6 月 22 日
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

採用された回答

Torsten
Torsten 2023 年 6 月 22 日
移動済み: Torsten 2023 年 6 月 22 日
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 件のコメント
Jon Bilbao
Jon Bilbao 2023 年 6 月 22 日
編集済み: Jon Bilbao 2023 年 6 月 22 日
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 2023 年 6 月 22 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFinancial Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by