The Newton Raphson method for p(x) = 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x, problem with derivative and reasonable x value
조회 수: 4 (최근 30일)
이전 댓글 표시
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:
x=[1598:1602];
p= 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x;
figure
plot (x, p)
xlabel('Material (x)')
ylabel('Profit (p)')
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
% Newton's 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;
x0 = 1601;
iterations = 10;
xnext = x0;
for i=1:iterations
xnext = xnext - f(xnext)/fdiff(xnext);
end
display("With " + iterations + " iterations of Newton's Method");
display(xnext);
display(abs(sqrt(2)-xnext));
And i have gotten the fdiff by doing the following in the command window
syms x
f=inline('100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x', 'x')
f =
Inline function:
f(x) = 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x
diff(f(x),x)
ans =
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)
% Define the function
f = @(x) 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x;
% Define the derivative of the function
f_prime = @(x) 50 .* (0.99.^(sqrt(x))) .* (log(0.99) ./ (2 * sqrt(x)) + 1 ./ (2 * sqrt(x))) - 0.5;
% Initial guess
x = initial;
number_of_iteration = 0;
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');
end
% Calculate the value of the function and its derivative
sum = f(x);
dif = f_prime(x);
if dif == 0
error('The derivative of the function is zero, pick another initial point and run again');
end
% Newton's formula
x = x - sum / dif;
end
root = x;
end
% Example usage
initial_guess = 1600; % Start around your known value
tolerance = 1e-5; % Define the desired tolerance
maxiteration = 100; % Maximum number of iterations allowed
[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.
댓글 수: 0
답변 (1개)
Sam Chak
2024년 10월 20일
편집: Sam Chak
2024년 10월 20일
Hi @Elina
You likely want to find the value of x where the maximum value of occurs, but you may have inadvertently used the Newton–Raphson method to find the root where .
If you want to find the maximum point, you should apply the Newton–Raphson method to find the root of . By the way, the derivative of the function (f_prime) in your original code appears to be incorrect. Note that the symbolic result for the derivative is 100% correct! Please double check.
The f function should be and f_prime should be .
p = @(x) 100*sqrt(x).*0.99.^(sqrt(x)) - 0.5*x;
dp = @(x) 50*(0.99.^sqrt(x))./sqrt(x) + log(0.99)*50*0.99.^sqrt(x) - 0.5;
x = linspace(1, 10000, 100000);
%% find the maximum point of p(x)
[val, idx] = max(p(x))
xmax = x(idx)
%% find the root of p(x)
proot = fzero(@(x) p(x), 7000)
%% Can use Newton–Raphson method to find the root of dp/dx
dproot = fzero(@(x) dp(x), 1500)
yyaxis left
plot(x, p(x))
grid on
yline(val, '--')
xline(xmax, '--')
xline(proot, '--')
xlabel({'$x$'}, 'interpreter', 'latex')
ylabel({'$p(x)$'}, 'interpreter', 'latex')
yyaxis right
plot(x, dp(x)), ylim([-0.75, 1]), grid on
ylabel({'$p''(x)$'}, 'interpreter', 'latex')
댓글 수: 4
Sam Chak
2024년 10월 20일
As for how I determined the derivative of , you actually computed it correctly on your own. I simply took your result and created a function handle for dp, which stands for the derivative of .
dp = @(x) 50*(0.99.^sqrt(x))./sqrt(x) + log(0.99)*50*0.99.^sqrt(x) - 0.5;
Your code:
syms x
f = inline('100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x', 'x')
df = diff(f(x), x)
Walter Roberson
2024년 10월 20일
It is recommended that you avoid using inline(). Replace it with
syms x
f = 100 .* sqrt(x) .* 0.99.^(sqrt(x)) - 0.5 .* x;
df = diff(f(x), x)
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!