0

I want to optimize a problem using Newton's Method in MATLAB, however, I am not getting a correct answer. I am hoping someone could me with my codes. The answer should be around 33.333 but I am getting 25.

clc; clear all; close all;

f = @(x) (5*sqrt(2))*(50-x)*(sqrt(2*x-50));  %Objective Function
df1 = @(x) - 10*(x - 25)^(1/2) - (5*(x - 50))/(x - 25)^(1/2);      % First Derivative
df2 = @(x) (5*(x - 50))/(2*(x - 25)^(3/2)) - 10/(x - 25)^(1/2);  % Second Derivative

K = 200;                 
x = 0;                    
Err = 0.0001;     

for i = 1:K               
    x = x - f(x)/df1(x);  
    j = i + 1;
    xx(j) = x;  

    err = abs(xx(j)-xx(j-1));   
    if err < Err
        break
    end
    z(i) = x;
end
x
  • Why is the 2nd derivative even mentioned if you're not using it? Did you try plotting this function (`f`)? How do you know the solution you're getting is incorrect? It appears that MATLAB is trying to minimize the function, whereas you'd like to maximize it. – Dev-iL Mar 29 '20 at 11:16
  • because previously I solved this problem using golden section search method and I got 33.333 and confirmed this using a matlab plot. I'll take your suggestion and convert it to a maximum model. – NoName123 Mar 29 '20 at 11:24
  • Note that for your initial guess (`x=0`) `f(x)` returns a complex value. Also, it appears that the current algorithm is designed to find zeros of `f` (at `25` and `50`), and you're looking for zeros of `df1`. – Dev-iL Mar 29 '20 at 11:36
  • so I should change it to x = x - df1(x)/df2(x)? – NoName123 Mar 29 '20 at 12:40

0 Answers0