# How to get matlab to create a function from system inputs and perform its fminsearch

1 view (last 30 days)
Nuttapong Sansuk on 2 Mar 2018
I want to use the simplex search method 'fminsearch' https://de.mathworks.com/help/matlab/ref/fminsearch.html
to find the minimum point of a function for example f(x(1),x(2)) = x(2) - n*x(1) while n could be any number depending on the input of system.
There were 2 methods i tried implementing
1. create symbolic valuables
x = sym('x_%d',[1 2]);
syms n;
f = x(2) - n*x(1);
and then i got
f = x_2 - n*x_1
but i dont know how to convert it to
f = @(x,n)(x(2) - n*x(1));
so i can perform
n = %input;
fun = @(x)f(x,n);
x0 = [1,1];
x = fminsearch(fun,x0)
2. execute
x0 = [1,1];
x = fminsearch(@objectivefcn1,x0)
by creating an objective function
function f = objectivefcn1(x)
f = x(2) - n*x(1);
end
anyway, i still can't find a way to pass the input n to the function without doing
function f = objectivefcn1(x,n)
f = x(2) - n*x(1);
end

Tony Mohan Varghese on 21 Mar 2018
Please refer the example for minimizing a function with extra parameters:

### Categories

Find more on Calculus in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!