Search code examples
mathmatlabsymbolic-math

How to solve equality equation through matlab solver?


I have a question related to basic mathematics, but with Matlab solving method. Here is the question:

y = 5;

for x=0.01:100
    F = 3*x + y - 2*x^2;
end

From the above iterative equation I want to find the max [F] value and its relative [x]. Is it possible to solve it through matlab solvers. Could you please help me to solve this problem?


Solution

  • For unconstrained non-linear optimization (according to your last edit) use fminsearch to solve your problem. It would be something like this:

    F = @(x) 3*x + y - 2*x^2;
    xini = 5; %initial value to the solver
    [xsolu Fsolu] = fminsearch(@F,xini)
    

    To control the options, parameters of solver see optimset

    opts = optimset('MaxFunEvals',10e4, 'MaxIter', 10e4)
    [xsolu Fsolu] = fminsearch(@F,xini, opts)
    

    The solution according to your original formulation would be:

    x=0.01:100;
    F = zeros(length(x),1);
    for ii = 1:length(x)
        F(i) = 3*x(i) + y - 2*x(i)^2;
    end
    
    xsolu = max(F);
    Fsolu = F(x == xsolu);
    

    Which is quite inefficient approach, to say nothing more.