How to find the value of 'x' which will minimize an enclaved function?

I am trying to use the function
[x,fval]=fminsearch(fun,x_0)
However the problem appears that i don't have any direct function (Direct means y= f(x), here you are seeing both y and x, in my case x is coming from a huge pile of programing) 'fun' in this case. My function is a combination of several steps. Here is my code that is generating the function ...
Rescaled_f = f/max(f);
Rescaled_g = g/max(f);
Model_number = (size(READ,2)/2)-1;
D = min(max(x_0_f),max(x_0_g)) - max(min(x_0_f),min(x_0_g));
Difference_Rescaled_fg = (Rescaled_f.-Rescaled_g);
fun = norm(Difference_Rescaled_fg)/(abs(D)*Model_number) %Here is my my function
As we are seeing there is no direct 'x' in my function then how can i minimize this function? Is it going to work like this....
x_0= 0.001 % Starting point
[x,fval]=fminsearch(@(x) fun,x_0)

4 Comments

Your function value depends on x, right? So you should be able to write it in terms of an input x. The example below is fine as well.
f=@(x) sin(x);
g=@(x) f(x);
h=@(x) g(x);
fun=@(x) g(x).^2./(h(x)+1.5);
x_0= 0.001 % Starting point
[x,fval]=fminsearch(@(x) fun,x_0)
It is not possible to summed up all the steps and make an Equation with x input.
Cause this f and g are coming from a huge pile of code.
If it possible to have the function header below, that would work as well.
function fval=fun(x)
If that is not possible your function doesn't depend on x, so you can't use fminsearch.
in my case x is coming from a huge pile of programing
If you try to minimize a function using fminsearch, e.g., x is not an output from a huge pile of programming, but the input to this programming producing a value that MATLAB tries to minimize.

Sign in to comment.

Answers (1)

There is no requirement in fminsearch that your function f(x) be expressed in a single one-line equation. fminsearch cares only that an input variable x goes into your function and that an output y=f(x) comes out.

8 Comments

So the way i wrote the code is this right?
You haven't shown us your complete code, so we cannot say authoritatively that it is right.
I cannot see here where you have implemented your objective function, what the input is and what the output is. The objective function should begin with a line like
function output=fun(x)
and have a line towards the end with something like
output = ...something...
I also cannot see where you have called fminsearch.
Then i wanted to implement this Equation
so that
d1_L2_pre
function is minimum.
i didn't yet used 'fminsearch', I am trying to do so.
Can you please help me with the objective function?
As you are seeing the main inputs are xf and xg. But i am only interested about xf. But the fact is for running this whole function you need both xf and xg. So how can i write
function output=fun(xf)
I edited my previous code. Can you please have a look?
The inputs are allowed to be vectors, so you can solve that issue like this:
wrapper=@(x_vector) fun(x_vector(1),x_vector(2));
function output=fun(xf,xg)
Also, you should keep your syntax Matlab-compatible on this forum, as Octave is more or less a competitor. So avoid endfunction and endif.
In the code you posted, xf will be overwritten:
xf=READ(:,1);
That line also suggests READ is a variable, not a function. That means your function is not self-contained.
The function you have as an input should run without error, at the very least for you x_0:
output=fun(x_0);
%x_0 can be a vector, output should be scalar

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 24 Oct 2018

Commented:

Rik
on 24 Oct 2018

Community Treasure Hunt

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

Start Hunting!