Problem with my Code?

[EDIT: 20110512 17:03 EDT - reformat - WDR]
I keep getting the error Undefined function or method 'Newton' for input arguments of type 'inline'. I'm doing newtons method and I can't figure out what that error means and what part of my code is wrong.
function x = Newton(f, fp, x, nmax, error)
x=1;
e=2.71828;
f(x)=inline('(x)*(5000)*(e^(-x)))-((100)+(0.73)*(5000)*(e^(-x))');
fp(x)=inline('-5000*(e^(-x))*(x-1.73)');
nmax = 10;
error = 1.0e-15;
x = Newton(f,fp,x,nmax,error)
fprintf('x(0) = %10g \n', x)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < error
fprintf('Converged! \n')
return
end
end
end

 Respuesta aceptada

Paulo Silva
Paulo Silva el 18 de Abr. de 2011

0 votos

There's no Newton function on MATLAB current path or any other path that MATLAB is aware, you get the same error for
whateverfunctionisayso(inline('x'))
This should be your script:
x=1;
f=inline('x*5000*exp(-x)-(100+0.73*5000*exp(-x))');
fp=inline('-5000*exp(-x)*(x-1.73)');
nmax = 10;
error = 1.0e-15;
x = Newton(f,fp,x,nmax,error)
fprintf('x(0) = %10g \n', x)
And this is your function:
function x = Newton(f, fp, x, nmax, error)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < error
fprintf('Converged! \n')
return
end
end
end
Save the function to the current path or another that MATLAB is aware of and run the script.

16 comentarios

Cote
Cote el 18 de Abr. de 2011
okay so completely get rid of the newton() parts?
Paulo Silva
Paulo Silva el 18 de Abr. de 2011
you are confused with the script that calls the function and the function
SCRIPT
x=1;
e=2.71828;
f(x)=inline('(x)*(5000)*(e^(-x)))-((100)+(0.73)*(5000)*(e^(-x))');
fp(x)=inline('-5000*(e^(-x))*(x-1.73)');
nmax = 10;
error = 1.0e-15;
x = Newton(f,fp,x,nmax,error)
fprintf('x(0) = %10g \n', x)
FUNCTION
function x = Newton(f, fp, x, nmax, error)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < error
fprintf('Converged! \n')
return
end
end
end
Cote
Cote el 19 de Abr. de 2011
Wait sorry how can I save the function to the current path and use it with the script?
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
just press the save button from the editor, the m file name should be the same as the function name and the folder (that opens first) is the current path, press save.
Cote
Cote el 19 de Abr. de 2011
So if my function is saved as Newton.m then I open a new file and use my script in there?
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
yes
Cote
Cote el 19 de Abr. de 2011
I can't get this to work, not quite sure what I'm doing wrong but I still think it has something to do with the way I'm saving the files and its directory
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
Use this script instead
x=1;
f=inline('x*5000*exp(-x)-(100+0.73*5000*exp(-x))');
fp=inline('-5000*exp(-x)*(x-1.73)');
nmax = 10;
error = 1.0e-15;
x = Newton(f,fp,x,nmax,error)
fprintf('x(0) = %10g \n', x)
Cote
Cote el 19 de Abr. de 2011
isn't this the same as before?
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
there were problems resulting in the error "unbalanced or unexpected parenthesis or bracket", works fine with that last code.
Cote
Cote el 19 de Abr. de 2011
So do I just use this in the editor right after the function x = Newton(f, fp, x, nmax, error) part
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
replace all the script that calls the function with the corrected one I posted, keep the function file the same.
Cote
Cote el 19 de Abr. de 2011
when I run the script and function together i get Error in ==> iscellstr whatever that means
Walter Roberson
Walter Roberson el 19 de Abr. de 2011
Exactly which line does it say that the problem is occurring at? When you put a breakpoint at that line and run again, what values do you see for the variables of that routine? When you go downwards in the call stack to find your routine, which line of the routine is it indicating invoked the problem? When you put a breakpoint at that line, then what values are you passing in to the call you see there? What happens when you delete all of your previous MATLAB Questions without even thanking the people who assisted you in solving your problem?
Cote
Cote el 19 de Abr. de 2011
Nevermind I got it to work thanks for all your help!
Cote
Cote el 19 de Abr. de 2011
And that thank you is directed towards Paulo and Matt

Iniciar sesión para comentar.

Más respuestas (1)

Matt Tearle
Matt Tearle el 18 de Abr. de 2011

0 votos

That message generally means that your function file isn't on the MATLAB path. Is Newton.m in your current directory?
There are some other issues with your code, but they wouldn't cause that error.

5 comentarios

Matt Tearle
Matt Tearle el 18 de Abr. de 2011
Looks like Paulo figured out what you were trying to do. So I'll just add that function handles are preferable to inline, and, either way, use y = exp(x) instead of e = 2.71828; y = e^x
Cote
Cote el 18 de Abr. de 2011
Thanks for the suggestion and that would mean I just need to change all of my e's to y's after setting y = exp(x) correct?
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
exp(1) equals to your e variable, that is 2.718281828459046
on the inline expressions use exp instead of the e variable
f(x)=inline('(x)*(5000)*(exp(-x)))-((100)+(0.73)*(5000)*(exp(-x))');
fp(x)=inline('-5000*(exp(-x))*(x-1.73)');
Paulo Silva
Paulo Silva el 19 de Abr. de 2011
Matt is also right about using function handles instead of inline expressions but I wouldn't bother much with it, all should work good with your current code.
Cote
Cote el 19 de Abr. de 2011
okay thank you

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Centro de ayuda y File Exchange.

Preguntada:

el 18 de Abr. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by