1

I have a function in 2 variables x1,x2

f = 3*x1^2 + 4*x2^2 + 5*x1 + 6*x2 + 10

Consider x is a row vector such that x = [x5,x6], where x5,x6 are components of the vector. If the notation is confusing, let us consider x = [x1,x1] but x1,x2 can be any arbitrary components. The same argument holds for y.

Then I want to find a from (x + ay) such that it will minimize the f. a is real constant, x and y are vectors. This is explained above.

If this does not make sense, then let us consider x,y as a 1-dimensional arrays with 2 locations. So, x(1),x(2),y(1),y(2) be their components. Then I want to multiply array y by a symbolic variable a.

For example, x=[4,5], y=[-2,3] then, (x + ay) = (4,5) + a(-2,3) = (4-2a,5+3a). a is symbolic variable that is unknown here.

Substituting in f1 (To be more clear, first argument in the definition of f x1 = 4-2a, second argument x2=5+3a)

f1 = 3*(4-2a)^2 + 4*(5+3a)^2 + 5*(4-2a) + 6*(5+3a) + 10 ............(eq. 1)

Then function f1 becomes unknown in one variable, a and can be minimized using 1D minimization algorithm, such as golden section search, given an interval [x_lower,x_upper].

My question is:

Given different x,y,

  • How to evaluate (x+ay) and pass (or substitute ?) it into function f (eq1)?
  • How to create 'dynamic' function f1, as in eq. 1, to pass it to 1D minimization algorithm? By dynamic, I mean here is function f1 will change every time for x,y.

I am interested in a low-level implementation of this problem (sticking to the basic features of a language as much as possible and without using language specific features or object oriented features) in python, MATLAB, C or any other language, but again in 'low level.' Can you suggest something?

UPDATE: I don't want to use symbolics from python, MATLAB or from any other language.

de23edced
  • 103
  • 1
  • 7
  • Is there a typo in you're first equation defining `f`? there is no `y` variable in it – Abstracted Jun 04 '16 at 18:08
  • @Abstracted No typo in `f`. It is function of `x,y` and then I want to pass `x+ay`. Is that answer your question? – de23edced Jun 04 '16 at 19:47
  • your function is wrongly stated, it has x, x1, x2 and you are creating an argument of (x+ay). Please write your question carefully. Currently the math is wrong. Because (x+ay) is a single argument but f is a function of (x,x1,x2). – percusse Jun 06 '16 at 01:47
  • @percusse, corrected typo. f is function of (x1,x2). There is nothing wrong in (x+ay). x and y are row vectors (with 2 variables) as stated in problem. – de23edced Jun 06 '16 at 11:09
  • @percusse. There was a typo in definition of `f` and I corrected it. Apart from that, there is nothing wrong. Please don't abuse power and downvote for wrong reasons. – de23edced Jun 06 '16 at 11:10
  • @de23edced I don't have power and didn't downvote. – percusse Jun 06 '16 at 11:41

2 Answers2

1

I'm rephrasing your question in my own words, because the question in its current form is confusing:

You have a function f(x1,x2) = 3*x1^2 + 4*x2^2 + 5*x1 + 6*x2 + 10. x1 and x2 are the components of a 2D vector obtained from summing x with the product of a and y, where x and y are given vectors, and a is a scalar. You want to obtain the function that results from substituting this relation into f.

Note that the notation is a bit confusing, so I will use instead x = z+a*y, where z (replacing the x you used) and y are the given vectors.

Let's define f as an anonymous function in Matlab (you could easily use a function file as well):

f = @(x) 3*x(1)^2 + 4*x(2)^2 + 5*x(1) + 6*x(2) + 10;

Note that I'm writing this differently than you did, i.e. x(1) and x(2) instead of x1 and x2. This means that I am using components of a vector instead of two unrelated variables.

Then, let's write your equation involving a as a function as well:

g = @(a) z + a*y;

The function g(a) returns a vector for each value a, obeying g(a) = z+a*y.

Now you can do the substitution:

h = @(a) f(g(a))

h is the desired function that takes a as input and returns the value of a applied at the vector obtained from z+a*y.

tvo
  • 780
  • 7
  • 24
-1

you can use eval convert string to function

f = 'x+a*y'
x = 4
y = 3
for a in xrange(3):
    print eval(f)

output:
4
7
10
galaxyan
  • 5,944
  • 2
  • 19
  • 43