![](/img/trans.png)
[英]Invoking a function from another which has the same number of inputs and outputs
[英]Using outputs from one function as inputs in another
我不知道为什么它不起作用,但我认为下面的代码对我来说效果很好:
function[f,fp,fpp,x_new]=fun(x)
f=exp(x) -3*x.^2 ;
fp=exp(x) -6*x ;
fpp=exp(x) -6;
x_new= x - 2*(f)*(fp) / 2*(fp.^2) -(f)*(fpp);
return
x=1234;
for ind=1:10
[f,fp,fpp,x]=fun(x);
end
我不确定这里的数学,但你只需要一个函数,它应该是这样的:
function r = halley(fun,x0,acc)
syms x
fp = diff(fun);
fpp = diff(fp);
x_new = symfun(x - 2*(fun(x))*(fp(x)) / 2*(fp(x).^2) -(fun(x))*(fpp(x)),x);
xold = x0;
xnew = x_new(xold);
while abs(fun(xold)) < acc && abs(xold-xnew) > acc
tmp = xnew;
xnew = x_new(xold);
xold = tmp;
end
r = xnew;
end
然后从另一个函数作为输入的脚本中调用它:
acc = 1.0e-8;
x0 = -5;
syms x
fun = symfun(exp(x) -3*x.^2 +1,x);
r = halley(fun,x0,acc)
但是,在本练习中,它说函数的输入应该是函数句柄,比如@fun
,所以也许你不应该使用符号数学? 我希望这会让事情变得更清楚,尽管我不知道这种方法应该如何运作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.