简体   繁体   中英

how to solve special equations in octave

I've got a porblem. I'm new in Octave need to solve these equations in this format:

-397.95 = min(k1*rate + k2); 776.37 = max(k1*rate + k2);

where rate is my row vector at size 10000. All I need is octave function which can deal with roots which are in other function (in my max and min). I know, that this question is a little bit mathematical, but I can't get the right easy function for solving this...

Thank you for answer

It looks like you need to use an optimization to minimise a cost function which would look like:

function y = f(x)

   % k1 is x(1), k2 is x(2)
   rate = ...
   y = [min(x(1)*rate + x(2))+397.95; max(x(1)*rate+x(2))-776.37]

end

You can then use optimization functions such as fminsearch or other (from the optim package). The idea is to try and minimize your cost function, ie get y towards 0. It might be a good idea to use abs in your function to avoid issue with negative numbers.

It's easy to see that the constraints for this problem are:

k1 * rate + k2 >= -397.95

and

k1 * rate + k2 <= 776.37

Since larger values of k1 give a larger variance in the result of this equation, your objective is to maximize k1 subject to these constraints (equivalently minimize -k1).

You can now run this as a simple linear program:

height = size(rate,1);
c = [-1;0];
A = [rate',ones(height,1); rate',ones(height,1)];
b = [-397.95*ones(height,1); 766.37*ones(height,1)];
lb = [0;-Inf];
ub = [Inf; Inf];
ctype = [repmat("L",height,1); repmat("U",height,1)];
k = glpk (c,A,b,lb,ub,ctype)
k1 = k(1);
k2 = k(2);

EDIT : I missed that rate was a row vector. I've transposed it appropriately

Actually since there are only two variables, this can be solved directly. Assuming k1 is positive (No reason not to make k1 positive since flipping the sign doesn't really change the problem since k2 can be shifted appropriately), then we have k1*max(rate) + k2 = 776.37 and k1*min(rate) + k2 = -397.95

So

k1*(max(rate) - min(rate)) = 776.37 - (-397.95)

Then we can solve for k1 as

k1 = (776.37 - (-397.95))/(max(rate) - min(rate))

And then k2 can be found as

k2 = 776.37 - k1 * max(rate)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM