تم الحل ✓
categoryهندسة الحاسبات
schoolبكالوريوس
event_available2026-07-16
السؤال
Transcribed Image Text:
Program 3-2: User-defined function. Newton's method.
function Xs = NewtonRoot (Fun, FunDer, Xest, Err, imax)
* NewtonRoot finds the root of Fun = 0 near the point Xest using Newton's method.
* Input variables:
% Fun Name of a user-defined function that calculates Fun for a given x.
* FunDer Name of a user-defined function that calculates the derivative
of Fun for a given x.
8
% Xest
Initial estimate of the solution.
% Err
Maximum error.
* imax
Maximum number of iterations
Output variable:
% Xs
Solution
for i=1: imax
end
Xi Xest
-
Fun (Xest)/FunDer (Xest);
Eq. (3.14).
if abs ((Xi
-
Xs = Xi;
Xest)/Xest) < Err
Eq. (3.18).
break
end
Xest = Xi;
if i = = imax
fprintf('Solution was not obtained in %i iterations.\n', imax)
Xs ('No answer');
end
x+1 =x-
8–4.5(x – sinx)
-4.5(1-cos.x;)
x2
=
2-
8-4.5(2-sin(2))- 2.48517
-4.5(1 cos(2))
X3
= 2.48517-
8-4.5(2.48517- sin (2.48517)) 2.43099
-4.5(1 cos(2.48517))
function y = FunExample2 (x)
-
y 8 4.5* (x
-
sin(x));
function y FunDerExample2 (x)
y = -4.5 +4.5*cos(x);
The user-defined functions are entered as function handles.
>> format long
>> xSolution = NewtonRoot (@Fun Example2, @FunDerExample2,2,0.0001, 10)
xSolution =
2.430465741723630
('x), f
x = 1+x
f(x))
f(x)
10
5
0
-5
-10
-155
0
1
2x
3
clear all
F(x) 8-4.5* (x-sin(x));
Define Ax) as an anonymous function.
a = 2; b = 3; imax = 20; tol = 0.001;
Fa = F(a); Fb = F(b);
if Fa*Fb > 0
Assign initial values to a and b, define
max number of iterations and tolerance.
disp (Error: The function has the same sign at points a and b.')
else
Stop the program if the
function has the same
sign at points a and b.
disp('iteration a b (xNS) Solution f(xNS) Tolerance')
for i = 1; imax
xNS
toli
(a+b)/2;
Calculate the numerical solution of the iteration, xNS.
Calculate the current tolerance.
Calculate the value of f(xNS) of the iteration.
FxNS
(ba)/2;
F(xNS);
fprintf('%3i %11.6f $11.6f $11.6f $11.6f $11.6f\n', i, a, b, xNS, FXNS, toli)
if FxNS == 0
fprintf('An exact solution x =11.6f was found',>NS)
end
break
if toli < tol
Stop the program if the true
solution, f(x) 0, is found.
break
end
if i = = imax
end
H
Stop the iterations if the tolerance of the iter-
ation is smaller than the desired tolerance.
fprintf('Solution was not obtained in &i iterations',imax)
break
if F(a) *FxNS < 0
b = xNS;
else
a = xNS;
end
end
end
Stop the iterations if
the solution was not
obtained and the num-
ber of the iteration
reaches imax.
Determine whether the true solution is
between a and xNS, or between xNS and b.
and select a and b for the next iteration.
iteration
a
b
(xNS) Solution
1 2 3
2.000000
2.000000
2.250000
3.000000
2.500000
f(xNS)
-0.556875
Tolerance
0.500000
2.500000
2.250000
1.376329
0.250000
2.500000
2.375000
0.434083
0.125000
4
2.375000
2.500000
2.437500
-0.055709
0.062500
5
2.375000
2.437500
2.406250
0.190661
0.031250
6
2.406250
2.437500
2.421875
0.067838
0.015625
10
7899
2.421875
2.437500
2.429688
0.006154
0.007813
2.429688
2.437500
2.433594
-0.024755
0.003906
2.429688
2.433594
2.431641
-0.009295
0.001953
2.429688
2.431641
2.430664
-0.001569
0.000977
The numerical solution.
The value of the function
at the numerical solution.
The last tolerance (satisfies
the prescribed tolerance).
SNX
9+0
Tolerance =
b-a
2
x
x
rsina
α
3r
x
4
r
α
C
α
x
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.