CS计算机代考程序代写 function newton_raphson_example

function newton_raphson_example

a = 9;
f = @(x) a*x.^2+(1-a)*x;
dfdx = @(x) 1-a+2*a*x;

% f = @(x) x.^2;
% dfdx = @(x) 2*x;

x_i = [0.6]; % Initial Condition
tol = 10^-6; % Tolerance of 10e-6
max_iter = 100; % This line is prevents the method from iterating in the case it diverges

i = 1;

xlimits = [0.5,1.5];
ylimits = [-2,3];

t = 0.5;

x = linspace(xlimits(1),xlimits(2),100);
f1 = f(x);
f_tangent = f(x_i(i)) + dfdx(x_i(i)).*(x-x_i(i));

figure(‘Visible’,”on”)

plot([-1,2],[0,0],’k’);
xlim(xlimits)
ylim(ylimits)
hold on
plot(x,f1);
pause(t)
plot([x_i(i)],[0],’r.’,’MarkerSize’,20)
str2 = sprintf(‘x_{%d}’,i-1);
text(x_i(i)+0.05,-0.1,str2)
pause(t)
plot([x_i(i),x_i(i)],[0,f(x_i(i))],’r–‘)
pause(t)
plot([x_i(i)],[f(x_i(i))],’r.’,’MarkerSize’,20)
str3 = sprintf(‘f(x_{%d})’,i-1);
text(x_i(i)+0.05,f(x_i(i))+0.05,str3)
pause(t)
plot(x,f_tangent);

while abs(f(x_i(i)))>tol && i