Borrar filtros
Borrar filtros

use f=@(x) as an argument of a function.m

68 visualizaciones (últimos 30 días)
DDD
DDD el 13 de Mzo. de 2015
Respondida: Matt J el 13 de Mzo. de 2015
I have the function
function [ k ] = New_Raph( f,df,x,tol)
x_old =x;
x=1000;
while abs(x_old-x) > tol
x_old = x;
delta=-f(x)/df(x);
x= x-delta;
end
k=x;
end
And i want to calculate in a new .m file:
f=@(x) x.^5-0.4475.*x.^4-3.1.*x.^3+3.085.*x.^2-0.962.*x+0.0943;
df1=@(x) 5.*x.^4-1.79.*x.^3-9.3.*x^2+6.17.*x-0.962;
New_Raph(@f1,@df1,0,0.1)
What is wrong?

Respuesta aceptada

per isakson
per isakson el 13 de Mzo. de 2015
Editada: per isakson el 13 de Mzo. de 2015
Replace
New_Raph(@f1,@df1,0,0.1)
by
New_Raph(f,df1,0,0.1)
f or f1 - a typo?
&nbsp
Addendum triggered by &nbsp It did not work
Try this example
>> my_sin = @(x) sin(x)
my_sin =
@(x)sin(x)
>> arrayfun( my_sin, pi/6*[1,2,3] )
ans =
0.5000 0.8660 1.0000
>> arrayfun( @sin, pi/6*[1,2,3] )
ans =
0.5000 0.8660 1.0000
and
>> arrayfun( @my_sin, pi/6*[1,2,3] )
Error: "my_sin" was previously used as a variable, conflicting with its use
here as the name of a function or command.
  2 comentarios
DDD
DDD el 13 de Mzo. de 2015
Editada: DDD el 13 de Mzo. de 2015
It did not work. I tryed making f1.m and df1.m and it solved but in this way it is much cleaner.
Stephen23
Stephen23 el 13 de Mzo. de 2015
"It did not work" does not tell us what happened when you tried it: was there an error message, unexpected values or something else?
It should work, according to the information that you have given us. Can you show us your exact code?

Iniciar sesión para comentar.

Más respuestas (1)

Matt J
Matt J el 13 de Mzo. de 2015
x= x + delta;

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by