Currently trying to write a function which is using the outputs of a previous function. The outputs are as follows:
f(x) = exp(x) -3*x.^2
fp(x) = exp(x) -6*x
fpp(x) = exp(x) -6
The new function is:
x_new = x - 2*(f(x))*(fp(x)) / 2*(fp(x).^2) -(f(x))*(fpp(x))
Tried to write [f,fp,fpp] = fun(x)
because it worked for me in a script file earlier but now its saying
error not enough input arguments
Any ideas greatly appreciated.
The original question:
I don't know why it is not working but I think code below works well for me:
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
I'm not sure about the math here, but you need only one function, that should look something like this:
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
and then you call it from a script with another function as input:
acc = 1.0e-8;
x0 = -5;
syms x
fun = symfun(exp(x) -3*x.^2 +1,x);
r = halley(fun,x0,acc)
however, in this exercise it says that the input of the function should be a function handle, like @fun
, so maybe you should not use symbolic math? I hope this makes things clearer, though I don't know how this method should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.