I have the below matlab code for newton raphson method.
a=1;b=4;c=-2;d=0;h=0.5;t=1;
x0 = -0.5+2i;
i = 1;
N = 100; %maximum number of iterations
tol = 1e-4; %precision required
syms x
p = @(x) x^2+a*x+b-c*exp(-x*h)-d*exp(-x*t);
f = @(x) a*x+b-lambertw(a*x+b-p*exp(a*x+b)); %function we are solving
df = diff(f); %differential of f(x)
while i <= N
numf = subs(f,x,x0); %// Numerator - Substitute f(x) for f(y)
denf = subs(df,x,x0);
x = x0-double(numf)/double(denf); %Newton-Raphson method
if (abs(x - x0)/abs(x))>tol %stopping criterion
fprintf('Solution is %f \n', double(x))
return
end
i = i + 1;
x0 = x; %update p0
end
fprintf('Solution did not coverge within %d iterations at a required precision of %d \n', N, error) %error for non-convergence within N iterations
When I ran it it gives the errors below:
- Error using mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
- Error using diff Function 'diff' is not supported for class 'function_handle'. Error in line 10 diff = diff(f);
%differential of f(x)
I don't know how to fix these errors. Help is truly appreciated.