-4

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?

Lab
  • 11
  • 1
  • 3
  • 1
    Could you be please more specific and/or revise your question, code? e.g. `F` is not affected by the loop, moreover, with the given conditions `F` is a linear function... – rozsasarpi Nov 15 '14 at 16:10
  • sorry, i edited the question. Now F is changing in each iteration – Lab Nov 15 '14 at 16:55
  • It is still a linear function. – rozsasarpi Nov 15 '14 at 17:00
  • But the main problem is how to find max value of Function and its responding x value. Cause, applying the simple max() function does not show its responding x, or i do not know how to implement it. And this is the basic equation, the real problem is non linear. – Lab Nov 15 '14 at 17:13
  • I think [this](http://www.mathworks.com/help/optim/ug/choosing-a-solver.html) might be helpful. – rozsasarpi Nov 15 '14 at 17:15
  • Use linear programming. Arpi's link above is useful. – rayryeng Nov 15 '14 at 17:16
  • Noted, I will try with them also. However, as i know the main limitation in this algorithms are their iteration number. Ex: 200 max iteration, which is too low for my original problem. – Lab Nov 15 '14 at 17:25
  • @Lab Your question is very unclear. What, exactly, is the function that you're trying to maximize? Is it a function of one (`x`) or two variables (`x` and `y`)? Is `y` fixed or not? If `y` is fixed and you want to maximise the function that, to a scalar `x`, associates `3*x + y - 2*x^2`, you can do that analytically... – jub0bs Nov 15 '14 at 17:26
  • The function is non linear and Y is fixed simple value, so there is just one variable that effects on changing of F function. And I want to go through extensive iteration to find the maximum F value and its responding x. Example: for x=0.01 value the F=15; when x=0.5, F=13; x=1, F=16. Hope you got what i want to say – Lab Nov 15 '14 at 17:54
  • @Lab Nope; your question still doesn't make any sense. If you evaluate your function of one variable at 0.01, you get a value. What is left to maximize? – jub0bs Nov 15 '14 at 18:49

1 Answers1

2

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.

rozsasarpi
  • 1,621
  • 20
  • 34
  • 1
    Really? `fminsearch` for a quadratic function? A bit overkill... – jub0bs Nov 15 '14 at 17:32
  • 2
    He/she changed the `F` function three times up to now, therefore I provided a general solution. – rozsasarpi Nov 15 '14 at 17:34
  • I agree. The OP can't seem to make up their mind, so give them an overkill tool and let them figure it out for themselves. – rayryeng Nov 15 '14 at 17:58
  • Thank you for the solution. But are their any simple method, where i can use basic iterative algorithm, cause these algorithms are time consuming – Lab Nov 15 '14 at 17:59
  • Yes. You can use Newtons method, but that's assuming your function is differentiable. You can check my post on the Newton Raphson method here: http://stackoverflow.com/a/25130693/3250829 – rayryeng Nov 16 '14 at 06:46