⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newtraph.mht

📁 it is a very essential matlab code.
💻 MHT
字号:
From: <Saved by Windows Internet Explorer 7>
Subject: 
Date: Tue, 12 May 2009 09:43:49 -0700
MIME-Version: 1.0
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: 7bit
Content-Location: http://www.mece.ualberta.ca/Courses/mec390/390code/newtraph.m
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.6000.16825" name=GENERATOR></HEAD>
<BODY><PRE>% Newton-Raphson root finding - single function
%
% The function and its derivative are defined in another script
% file as follows: (Note that this won't accept vector inputs)
%
%   function [func, deriv] = fcn_nr(x)
%       func = x^4 - 5*x^3 - 124*x^2 + 380*x + 1200;
%       deriv = 4*x^3 - 15*x^2 - 248*x + 380;

% First, let's plot the function.
clear; clf; xp = -12:1:15; n = length(xp);
for i=1:n
  yp(i) = fcn_nr(xp(i));
end
quickplt(xp,yp,'x','f(x)','Plot of function','grid')
hold on

% now proceed with finding a root:

x = input('Enter initial guess of root location: ');

itermax = 100;      % max # of iterations
iter = 0;
errmax = 0.001;     % convergence tolerance
error = 1;
fprintf('\n   #      root       rel error\n\n');

while error &gt; errmax &amp; iter &lt; itermax

  iter = iter + 1;
  [f fprime] = fcn_nr(x);
  if fprime == 0
    fprintf('ERROR: deriv(x) = 0; can''t divide by zero\n')
    break;
  end;

  xnew = x - f / fprime;    % here is new root estimate

  error = abs((xnew - x)/xnew) * 100;  % find change from previous
  fprintf('%3i   %10.6f   %10.5f\n',iter,xnew,error);
  x = xnew;      % set up for next iteration

end
plot(x, fcn_nr(x),'ro')

</PRE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -