Error code line 19 with my Y=f(x), equation used is x^3+x^2-4x-4
function [x,y,err]=muller(f,x0,x1,x2,delta,epsilon,max1)
%Input – f is the object function
% – x0, x1, and x2 are the initial approximations
% – delta is the tolerance for x0, x1, and x2
% – epsilon the the tolerance for the function values y
% – max1 is the maximum number of iterations
%Output- x is the Muller approximation to the zero of f
% – y is the function value y = f(x)
% – err is the error in the approximation of x.
%Initalize the matrices X and Y
X=[x0 x1 x2];
Y=f(X);
%Calculate a and b in formula (15)
for k=1:max1
h0=X(1)-X(3);h1=X(2)-X(3);e0=Y(1)-Y(3);e1=Y(2)-Y(3);c=Y(3);
denom=h1*h0^2-h0*h1^2;
a=(e0*h1-e1*h0)/denom;
b=(e1*h0^2-e0*h1^2)/denom;
%Find the smallest root of (17)
if b < 0
disc=-disc;
end
z=-2*c/(b+disc);
x=X(3)+z;
%Sort the entries of X to find the two closest to x
if abs(x-X(2))<abs(x-X(1))
Q=[X(2) X(1) X(3)];
X=Q;
Y=f(X);
end
if abs(x-X(3))<abs(x-X(2))
R=[X(1) X(3) X(2)];
X=R;
Y=f(X);
end
%Replace the entry of X that was farthest from x with x
X(3)=x;
Y(3) = f(X(3));
y=Y(3);
%Determine stopping criteria
err=abs(z);
relerr=err/(abs(x)+delta);
if (err<delta)||(relerr<delta)||(abs(y)<epsilon)
break
end
endfunction [x,y,err]=muller(f,x0,x1,x2,delta,epsilon,max1)
%Input – f is the object function
% – x0, x1, and x2 are the initial approximations
% – delta is the tolerance for x0, x1, and x2
% – epsilon the the tolerance for the function values y
% – max1 is the maximum number of iterations
%Output- x is the Muller approximation to the zero of f
% – y is the function value y = f(x)
% – err is the error in the approximation of x.
%Initalize the matrices X and Y
X=[x0 x1 x2];
Y=f(X);
%Calculate a and b in formula (15)
for k=1:max1
h0=X(1)-X(3);h1=X(2)-X(3);e0=Y(1)-Y(3);e1=Y(2)-Y(3);c=Y(3);
denom=h1*h0^2-h0*h1^2;
a=(e0*h1-e1*h0)/denom;
b=(e1*h0^2-e0*h1^2)/denom;
%Find the smallest root of (17)
if b < 0
disc=-disc;
end
z=-2*c/(b+disc);
x=X(3)+z;
%Sort the entries of X to find the two closest to x
if abs(x-X(2))<abs(x-X(1))
Q=[X(2) X(1) X(3)];
X=Q;
Y=f(X);
end
if abs(x-X(3))<abs(x-X(2))
R=[X(1) X(3) X(2)];
X=R;
Y=f(X);
end
%Replace the entry of X that was farthest from x with x
X(3)=x;
Y(3) = f(X(3));
y=Y(3);
%Determine stopping criteria
err=abs(z);
relerr=err/(abs(x)+delta);
if (err<delta)||(relerr<delta)||(abs(y)<epsilon)
break
end
end function [x,y,err]=muller(f,x0,x1,x2,delta,epsilon,max1)
%Input – f is the object function
% – x0, x1, and x2 are the initial approximations
% – delta is the tolerance for x0, x1, and x2
% – epsilon the the tolerance for the function values y
% – max1 is the maximum number of iterations
%Output- x is the Muller approximation to the zero of f
% – y is the function value y = f(x)
% – err is the error in the approximation of x.
%Initalize the matrices X and Y
X=[x0 x1 x2];
Y=f(X);
%Calculate a and b in formula (15)
for k=1:max1
h0=X(1)-X(3);h1=X(2)-X(3);e0=Y(1)-Y(3);e1=Y(2)-Y(3);c=Y(3);
denom=h1*h0^2-h0*h1^2;
a=(e0*h1-e1*h0)/denom;
b=(e1*h0^2-e0*h1^2)/denom;
%Find the smallest root of (17)
if b < 0
disc=-disc;
end
z=-2*c/(b+disc);
x=X(3)+z;
%Sort the entries of X to find the two closest to x
if abs(x-X(2))<abs(x-X(1))
Q=[X(2) X(1) X(3)];
X=Q;
Y=f(X);
end
if abs(x-X(3))<abs(x-X(2))
R=[X(1) X(3) X(2)];
X=R;
Y=f(X);
end
%Replace the entry of X that was farthest from x with x
X(3)=x;
Y(3) = f(X(3));
y=Y(3);
%Determine stopping criteria
err=abs(z);
relerr=err/(abs(x)+delta);
if (err<delta)||(relerr<delta)||(abs(y)<epsilon)
break
end
end muller method MATLAB Answers — New Questions