[討論] 想請各位大大幫我看我寫的牛頓法程式
這題是書上經典的自由落體題目改過求cd的
--> t=4;
--> v=36;
--> g=9.81;
--> m=80;
--> xo=0.25;
--> Ea=0.001;
--> f=@(cd) sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)-v;
--> df=@(cd) -1/2*sqrt(g*m)*cd^(-3/2)*tanh(sqrt(g*cd/m)*t)+1/2*sqrt(g*m/cd)*
sqrt(g/m)*t*(sech(sqrt(g*cd/m)*t))^2;
[root,ea,iter]=newtraph(f,df,0.25,0.001,100)
--> root =
0.1401
ea =
8.1853e-004
iter =
35
請問我iter為甚麼會是35正確答案應該是4才對
因為我是用freemat所以微分是我自己算的
牛頓程式在這
function [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
% newtraph: Newton-Raphson root location zeroes
% [root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,p1,p2,...):
% uses Newton-Raphson method to find the root of func
% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by function
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin<3,error('at least 3 input arguments required'),end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
xrold = xr;
xr = xr - func(xr)/dfunc(xr);
iter = iter + 1;
if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
if ea <= es | iter >= maxit, break, end
end
root = xr;
另外想請問各位大大有關matlab的書
我的專題老師要我用matlab寫出一個使用者介面
請問各位前輩可以介紹哪本書有這方面的資訊嗎?(最好是中文本小弟英文很差)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.25.118.145
→
03/19 19:55, , 1F
03/19 19:55, 1F
→
03/19 21:27, , 2F
03/19 21:27, 2F