我有一个名为funcion(x)
的函数,它做:
function fx = funcion(x)
fx = cos(x); %%
return;
然后在我的另一个函数(它们都在同一个脚本中)中称为newtonRaph
我正在这样做:
function raiz = newtonRaph(xi,imax, tol)
syms x;
iter = 0;
xold = xi;
x = xold;
df = diff(cos(x),x);
er = 0.9;
while (er>tol)&&(iter<imax)
dfr = (subs(df,x,xold));
nuevo = 0.222/dfr;
if(dfr ==0)
disp('dfr was 0...');
break;
else
iter = iter+1;
evaluacion = funcion(x);
xnew = xold - (evaluacion/dfr); %Newton-Raphson formula
if(xnew~=0)&& (iter>1)
er = abs((xnew-xold)/xnew); %
end
xold = xnew;
x = xold;
end
end
root = xnew;
如您所见,我添加了一个测试线,它只是为了尝试查看导数发生了什么,它new = 0.222/dfr
。
我不知道我做错了什么,但每次我运行这个时,它都会告诉我
??? Error using ==> mldivide
Matrix dimensions must agree.
Error in ==> newtonRaph at 16
nuevo = 0.222/dfr;
如果有人能告诉我该怎么做,我将不胜感激。
如果dfr
不是标量,并且您只想将0.222
除以其中的所有元素,那么您应该在/
之前用.
编写nuevo = 0.222./dfr
。