How can i implement my method ?
2 ビュー (過去 30 日間)
古いコメントを表示
I know the classic newton method but i can not implement the fourier form please help me.
%Newton-Raphson method
clear;
clc
%first plot the function
plot(f) x=0:0.05:4;
f=@(x)(x^3)+(x^2)-(x)-1;
plot(x,f(x));
grid
fd=@(x)3*x^2+2*x-1;
x1=input ('x1=');
tol=0.000001;
i = 0;
while abs(f(x1)) > tol
f1=f(x1);
f1d=fd(x1);
x2=x1-(f1/f1d);
f2=f(x2);
x1=x2;
i = i + 1;
fprintf('%9.6f %13.6f \n',x2,f2)
end
3 件のコメント
採用された回答
Morgan
2022 年 11 月 6 日
Here is a function I've created that hopefully answers your question:
function [x,fval,fevals] = NewtonFourier(f,ab,tol)
% NEWTONFOURIER Function that will efficiently find the local zero
% of an arbitrary function within a specified initial
% guess and tolerance.
%
% [x,fval,fevals] = NEWTONFOURIER(f,x0,tol);
%
% INPUT ARGUMENTS
% ================
% f Function handle to function to be searched
% ab Interval [a,b] in which root is assumed to be
% tol Desired relative error
%
% OUTPUT ARGUMENTS
% ================
% x The coordinate where f(x) = 0
% fval The evaluation of f(x)
% fevals Number of times the func was evaluated
% CHECK IF PROBLEM IS LIKELY UNSOLVABLE
if f(ab(1))*f(ab(2)) > 0
warning('f(a)f(b) > 0, there likely is no solution in [a,b].');
end
% DETERMINE APPROXIMATE DERIVATIVES OF func
fp = @(x) (f(x+tol)-f(x-tol))/(2*tol);
% INITIALIZE fevals
fevals = 2;
% INITIALIZE xn AND zn
xn = ab(2);
zn = ab(1);
% MAIN LOOP
err = Inf;
while err > tol
% Update root estimates
fxn = f(xn)/fp(xn);
xn = xn - fxn;
fzn = f(zn)/fp(zn);
zn = zn - fzn;
% Determine if converged
err = abs((fxn+fzn)/2);
% Update function evaluation counter
fevals = fevals+6;
end
% RETURN OUTPUTS
x = xn;
fval = f(xn);
fevals = fevals+1;
end
I've also written a demo file that solves the specific problem you've referenced in your question here:
% demo_NewtonFourier.m
% Initialize MATLAB
clear variables
close all
clc
% Define NewtonFourier.m Inputs
f = @(x) x.^3 + x.^2 + 1;
tol = 1e-6;
ab = [ -2 +2 ];
% Plot Function
x = ab(1) : 0.05 : ab(2);
y = f(x);
figure(1);
hold on
plot(x,y,'-k','LineWidth',2);
plot(x,0*x,'--k','LineWidth',1);
hold off
xlabel('$x$','Interpreter','latex');
ylabel('$f(x)$','Interpreter','latex');
title(['$ f(x) = ' latex(f(sym('x'))) '$'],'Interpreter','latex');
set(gca, 'FontSize', 12, 'FontName', 'Times', ...
'XMinorTick', 'on', 'YMinorTick', 'on', ...
'TickLength', [0.015, 0.0015]);
% Call NewtonFourier.m
[x,fval,fevals] = NewtonFourier(f,ab,tol)
The outputs of this demo file include the following figure showing the only real solution to be approximately -1.46,
And
x =
-1.4656
fval =
4.4409e-16
fevals =
153
If you require explanation of the code snippets or need anything else, let me know!
- Morgan Blankenship, B.S., M.S., EIT
4 件のコメント
Walter Roberson
2022 年 11 月 6 日
Are you prepared to pay Morgan private consulting fees for providing a private solution to your question?
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Number Theory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!