用matlab编程牛顿法求x-cosx=0的实根

2025-01-31 12:48:43
推荐回答(1个)
回答1:

function x=NewRaph(x0,f,fprime,tol)
x(1)=x0;
i=1;
gx=x(i)-f(x(i))/fprime(x(i));
while(abs(x(i)-gx)>tol)
gx=x(i);
x(i+1)=x(i)-f(x(i))/fprime(x(i));
i=i+1;
end

调用:y=NewRaph(1,@(x)x-cos(x),@(x)1+sin(x),10^-6)
y(end)即为逼近值