Effacer les filtres
Effacer les filtres

Problem with my Code?

12 vues (au cours des 30 derniers jours)
Cote
Cote le 18 Avr 2011
[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

Réponse acceptée

Paulo Silva
Paulo Silva le 18 Avr 2011
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 commentaires
Cote
Cote le 19 Avr 2011
Nevermind I got it to work thanks for all your help!
Cote
Cote le 19 Avr 2011
And that thank you is directed towards Paulo and Matt

Connectez-vous pour commenter.

Plus de réponses (1)

Matt Tearle
Matt Tearle le 18 Avr 2011
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 commentaires
Paulo Silva
Paulo Silva le 19 Avr 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 le 19 Avr 2011
okay thank you

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by