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
采纳的回答
更多回答(1 个)
Matt Tearle
2011-4-18
0 个投票
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 个评论
Matt Tearle
2011-4-18
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
2011-4-18
Paulo Silva
2011-4-19
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
2011-4-19
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
2011-4-19
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!