Question

Using the "Newton's Method" Write a MATLAB script to solve for the following nonlinear system of...

Using the "Newton's Method"

Write a MATLAB script to solve for the following nonlinear system of equations:

x2 + y2 + z2 = 3

x2 + y2 - z = 1

x + y + z =3

using the initial guess (x,y,z) = (1,0,1), tolerance tol = 1e-7, and maximum number of iterations maxiter = 20.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. MATLAB code

f1name = input('The first function is (between single quotes): f1(x,y,z) = ');
f2name = input('The second function is (between single quotes): f2(x,y,z) = ');
f3name = input('The third function is (between single quotes): f3(x,y,z) = ');
gradf1 = [diff(f1name,'x') diff(f1name,'y') diff(f1name,'z')] ;
gradf2 = [diff(f2name,'x') diff(f2name,'y') diff(f2name,'z')];   
gradf3 = [diff(f3name,'x') diff(f3name,'y') diff(f3name,'z')] ;
wa = input('Initial values [xo yo zo] = ');wa= wa';
tol = input('Tolerance: ');
nitermax = input('Number of iterations k = ');
t1 = 'The first function is: f1(x,y,z) = ';
t2 = 'The second function is: f2(x,y,z) = ';
t3 = 'The third function is: f3(x,y,z) = ';
disp([t1 f1name]);   
disp([t2 f2name]);
disp([t3 f3name]);   
disp(fprintf('The initial values are xo = %- 4.2f, yo = %- 4.2f and zo = %- 4.2f',wa(1),wa(2),wa(3)))
disp(fprintf('The maximun number of iteration is k = %3.0f\n and the tolerance is: %4.2e', nitermax,tol))
disp('-----------------------------')
disp('')
disp(' k x(k) y(k) z(k) f1(x(k),y(k),z(k)) f2(x(k),y(k),z(k)) f3(x(k),y(k),z(k))')
disp('---------------------------------------------------------------------------------------------------------------------------------')
x = wa(1);
y = wa(2);
z=wa(3);
fw1 = eval(f1name);
fw2 = eval(f2name);
fw3 = eval(f3name);
fxo = [fw1;fw2;fw3];
f1pw = eval(gradf1);
f2pw = eval(gradf2);
f3pw = eval(gradf3);
fpxo =[f1pw;f2pw;f3pw];
if det(fpxo)==0
disp('Error: determinant of Jacobian is zero, try another initial point ')
else
iter = 0;
s1 = sprintf('%3.0f %- 10.4f %- 10.4f %- 10.4f',iter,wa(1),wa(2),wa(3));
s2 = sprintf(' %- 10.4f %- 10.4f %- 10.4f',fw1,fw2,fw3);
disp([s1 s2])
while (norm(fxo)>tol) & (det(fpxo)~=0) & (iter<nitermax)
iter = iter + 1;
wan = wa - inv(fpxo)*fxo;
wa = wan;
x = wa(1);
y = wa(2);
z = wa(3);
fw1 = eval(f1name);
fw2 = eval(f2name);
fw3 = eval(f3name);
fxo = [fw1;fw2;fw3];
f1pw = eval(gradf1);
f2pw = eval(gradf2);
f3pw = eval(gradf3);
fpxo =[f1pw;f2pw;f3pw];
if det(fpxo)==0
disp('Error: determinant of Jacobian is zero, try another initial point ')
end
s1 = sprintf('%3.0f %- 10.4f %- 10.4f %- 10.4f',iter,wa(1),wa(2),wa(3));
s2 = sprintf(' %- 10.4f %- 10.4f %- 10.4f',fw1,fw2,fw3);
disp([s1 s2])
end
root = wa;
disp(fprintf('\n The Root are: x = %- 6.4f, y = %- 6.4f, z = %- 6.4f',wa(1),wa(2),wa(3)))
end   

Add a comment
Know the answer?
Add Answer to:
Using the "Newton's Method" Write a MATLAB script to solve for the following nonlinear system of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm working on the newton's method on matlab, could someone help me and show what two...

    I'm working on the newton's method on matlab, could someone help me and show what two lines are needed to be added in the codes in order to make this function work? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x)...

  • Problem Two: (Based on Chapra, Problems 12.9 Consider the simultaneous nonlinear equations: 2-5-y y+i- 1. Plot...

    Problem Two: (Based on Chapra, Problems 12.9 Consider the simultaneous nonlinear equations: 2-5-y y+i- 1. Plot the equations and identify the solution graphically. Page 1 of 2 2. Solve the system of equations using successive substitution, starting with the initial guess xo-y-1.5. Show two complete iterations. Evaluate &s for the second iteration. 3. Redo Part 2 using Newton-Raphson method . Automate the solutions in Parts 2 and 3 using MATLAB scripts 5. Solve the system of nonlinear equations by calling...

  • Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

    Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root ^n and the number of steps n taken to reach the solution. Use function mynewton.m to perform...

  • How to write in matlab program ? (1) Reduce the following system of equations to one equation in ...

    How to write in matlab program ? (1) Reduce the following system of equations to one equation in terms of x and solve the resulting equation numerically using Newton-Raphson method. ex/10 – y = 0 and 2logey – cosx = 2 (2) Solve the above equations numerically using system of equations, first by plotting the graph to obtain an initial approximation of the roots (such as x = 1 with y = 1 or other combinations), then produce the results...

  • Please use MATLAB to solve the question Computer Problem. Solve fi(x) = x – cos(x) =...

    Please use MATLAB to solve the question Computer Problem. Solve fi(x) = x – cos(x) = 0 and f2(x) = x2 – 2xcos(x) + cos²(x) = 0 both with initial guess xo = 0 by using Newton's mehtod and fill the following table. Stopping tolerance 10-5 fi(x) = 0 # of iterations Root $2(x) = 0 # of iterations Root 10-6 10-8 10-10

  • (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so...

    (a) (4 points) Fill in the blanks in the following MATLAB function M file trap so that it implements the composilu trapezidul rulo, using a soquence of w -1,2, 4, 8, 16,... trapezoids, to approximatel d . This M file uses the MATLAB built-in function trapz. If 401) and y=() f(!)..... fr. 1)). 3= ($1, then the execution of z = trapz(x, y) approximates using the composite trapezoidal rule (with trapezoids). (Note that the entries of are labeled starting at...

  • Could someone explain these four promblems on matlab and if you do, could you write what...

    Could someone explain these four promblems on matlab and if you do, could you write what you wrote on matlab I.e. on the command window or script. Also if you have written anything by hand can you write neatly. Also an explain of how you did it would be greatly appreciated. 1] 5 points) Write the following set of equations in Matrix form and use Matlab to find the solution. 50 -5x3-6x2 2x2 + 7x3-30 x1-7x3-50-3x2 + 5x1 [2] (10...

  • Chapter 04.08:Problem #1 Solve the following system of equations using the Gauss-Seidel method. 1 12х, + 7х, + 3x, %317...

    Chapter 04.08:Problem #1 Solve the following system of equations using the Gauss-Seidel method. 1 12х, + 7х, + 3x, %317 Зх, + 6х, + 2х, %3D9 2x, + 7x, -11х, %3D 49 Conduct 3 iterations. Calculate the maximum absolute relative approximate error at x x[ 3 s] as your initial guess the end of each iteration. Choose Chapter 04.08:Problem #1 Solve the following system of equations using the Gauss-Seidel method. 1 12х, + 7х, + 3x, %317 Зх, + 6х,...

  • In MATLAB please Consider the nonlinear function: y = f(x) = x3 cos x a. Plot...

    In MATLAB please Consider the nonlinear function: y = f(x) = x3 cos x a. Plot y as a function of x as x is varied between -67 and 67. In this plot mark all the locations of x where y = 0. Make sure to get all the roots in this range. You may need to zoom in to some areas of the plot. These locations are some of the roots of the above equation. b. Use the fzero...

  • 4. (20 pts) In this problem, we combine the Steepest Descent method with Newton's method for solv...

    4. (20 pts) In this problem, we combine the Steepest Descent method with Newton's method for solving the following nonlinear system. en +en-13 = 0, 12-2113 = 4. Use the Steepest Descent method with initial approximation x0,0,0 three iterations x(1), x(2), and x(3) to find the first ·Use x(3) fron the above the result as the initial approximation for Newton's iteration. Use the stopping criteria X(k)-s(k 1) < tol = 10 9 Display the results for each iteration in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT