function oppgave_1_2_6 f = @(x) exp(x-2)+x.^3 - x; % fixed point funciton and its derivative g1 = @(x) exp(x-2)+x.^3; dg1 = @(x) exp(x-2) + 3*x.^2; % fixed point funciton and its derivative g2 = @(x) (x-exp(x-2)).^(1/3); dg2 = @(x) 1/3*(x-exp(x-2)).^(-2/3).*(1-exp(x-2)); % fixed point funciton and its derivative % this one is derived from exp(x-2) + x*(x-1)*(x+1) = 0 g3 = @(x) -1 -exp(x-2)./(x.^2-x); dg3 = @(x) -(exp(x-2).*(x.^2-x)-exp(x-2).*(2.*x-1))./(x.^2-x).^2; % plot the function to visually find the roots X = linspace(-1.5,2,100); figure(1); plot(X,f(X)); grid on; xlabel('x'); ylabel('f(x)'); figure(2); plot(X,X,X,g1(X)); grid on; xlabel('x'); ylabel('g1(x)'); legend('x','g1(x)'); figure(3); plot(X,X,X,g2(X)); grid on; xlabel('x'); ylabel('g2(x)'); legend('x','g2(x)'); figure(4); plot(X,X,X,g3(X)); grid on; xlabel('x'); ylabel('g3(x)'); legend('x','g3(x)'); fprintf('\n\n\nTrying g1\n\n\n'); run_fpi(f,g1,dg1); fprintf('\n\n\nTrying g2\n\n\n'); run_fpi(f,g2,dg2); fprintf('\n\n\nTrying g3\n\n\n'); run_fpi(f,g3,dg3); function run_fpi(f,g,dg) % try fpi from different starting points x = 0.75; x = fpi(f,g,x); fprintf('x = %e, f(x) = %e, dg(x)=%e\n', x,f(x),dg(x)); x = 0.1; x = fpi(f,g,x); fprintf('x = %e, f(x) = %e, dg(x)=%e\n', x,f(x),dg(x)); x = -1.5; x = fpi(f,g,x); fprintf('x = %e, f(x) = %e, dg(x)=%e\n', x,f(x),dg(x)); function x=fpi(f,g,x) e0 = f(x); for i = 1:100, x1=g(x); if ~isfinite(x1) break; end e1 = abs(f(x1)); %if e1 < 1.0E-10, % break; %end if abs(x1-x)/max(abs(x),1.0) < 1.0E-06, break; end fprintf('e1/e0 = %e\n', e1/e0); x = x1; e0 = e1; end