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.

6
  • Is there a typo in you're first equation defining f? there is no y variable in it Commented Jun 4, 2016 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? Commented Jun 4, 2016 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). Commented Jun 6, 2016 at 1: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. Commented Jun 6, 2016 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. Commented Jun 6, 2016 at 11:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answer. I have rephrased it. Now it should be less confusing.
Let me know if question still sounds confusing.
@de23edced Your question is still confusing. Is my rephrasing of your question correct? Did my answer satisfy your needs?
-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.