Hi!
I'm very new to coding an MATLAB and only understand about half of what I'm doing and would really apricate some help.
I'm trying to solve
p (x) = 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x
numerically. I have graphed it and found that the critical point x should be around 1600, se picture
Using the code:
p= 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x;
Now I'm trying to find the exact x and I have found a lot of diffrent codes online to build the The Newton Raphson method, but are very confused as there is so many ways of doing it. I have tried a couple of diffrent code, but non have worked. Or they have given me values but the valus for x are nowhere near 1600. And I don't understand what is wrong, I'm thinking it might not be the code but the derivative I get wrong, or somthing.
This is one of the scripts I have tried for Newton method
f = @(x) 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x;
fdiff = @(x) 50*(99/100)^(x^(1/2))*log(99/100) + (50*(99/100)^(x^(1/2)))/x^(1/2) - 1/2;
xnext = xnext - f(xnext)/fdiff(xnext);
display("With " + iterations + " iterations of Newton's Method");
display(abs(sqrt(2)-xnext));
And i have gotten the fdiff by doing the following in the command window
f=inline('100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x', 'x')
f(x) = 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x
50*(99/100)^(x^(1/2))*log(99/100) + (50*(99/100)^(x^(1/2)))/x^(1/2) - 1/2
If I try to run this I get
newtons_method
"With 10 iterations of Newton's Method"
xnext =
3.3402e-05 - 5.7903e-24i
1.4142
It's saying x is 3.3402e-05 - 5.7903e-24i
which isn't reasonable.
If I try this code wich is a code I found online and that I asked ChatGPT to help me taylor to my function (I know its not the most reliable, but at this point I don't know what to do).
function [root, number_of_iteration] = newton_profit(initial, tolerance, maxiteration)
f = @(x) 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x;
f_prime = @(x) 50 .* (0.99.^(sqrt(x))) .* (log(0.99) ./ (2 * sqrt(x)) + 1 ./ (2 * sqrt(x))) - 0.5;
while abs(f(x)) > tolerance
number_of_iteration = number_of_iteration + 1;
if number_of_iteration > maxiteration
error('Failed to converge, the maximum number of iterations is reached');
error('The derivative of the function is zero, pick another initial point and run again');
[root, num_iterations] = newton_profit(initial_guess, tolerance, maxiteration);
disp(['Root: ', num2str(root)]);
disp(['Number of Iterations: ', num2str(num_iterations)]);
I then get
Root: 7235.8192
Number of Iterations: 15
Witch also isn't reasonable.
Remeber the graph, it should def be around 1600
This is my first cours in math at university so I might also have missed something important in the math. But the answer should be around 1600 right? Can somone please help me to understand what is going wrong and help me write a new code. Thank you in advance.