Skip to content Skip to sidebar Skip to footer

Implementing Minimization Method

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 not

Solution 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.

Solution 2:

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

Post a Comment for "Implementing Minimization Method"