In: Computer Science
Write your own MATLAB code to solve the system
10 − x + sin(x + y) − 1 = 0
8y − cos2 (z − y) − 1 = 0
12z + sin(z) − 1 = 0
using a residual tolerance of 10^−6 and the initial guess, ~x 0 = [0.1, 0.25, 0.08]^T . Print out the values for x, y and z for each iteration in a table similar to the one you created for the problems of the previous homework. You should submit your code (which can again be organized as a function and the script calling this function) together with your output.
clc
clear all
close all
format long
ff=@(x,y,z) [10-x+sin(x+y)-1;8*y-cos(z-y)^2-1;12*z+sin(z)-1];
f=@(x) ff(x(1),x(2),x(3));
g=@(x,y,z) [cos(x + y) - 1, cos(x + y), 0;0, 2*cos(y - z)*sin(y - z) + 8, -2*cos(y - z)*sin(y - z);0, 0, cos(z) + 12];
J=@(x) g(x(1),x(2),x(3));
x0=[0.1;0.25;0.08];
for i=1:100
x1=x0-inv(J(x0))*f(x0);
fprintf('Iteration %d, x=%f, y=%f, z=%f\n',i,x1);
if(norm(x1-x0)<1e-6)
break;
end
x0=x1;
end