Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please translate this Newton Raphson Matlab code to Scilab. There are three .m f

ID: 2963044 • Letter: P

Question

Please translate this Newton Raphson Matlab code to Scilab. There are three .m files. I was able to translate no 2 and 3 but fail to translate no 1.


1) NR.m

function x = NR(fnctn,x0,tol,trace,varargin)

%NR Finds a zero of a function by the Newton-Raphson method.

%

% NR('F',X0) finds a zero of the function described by the

% M-file F.M. X0 is a starting guess.

%

% NR('F',X0,TOL,TRACE) uses tolerance TOL for convergence

% test. TRACE=1 shows the calculation steps numerically and

% TRACE=2 shows the calculation steps both numerically and

% graphically.

%

% NR('F',X0,TOL,TRACE,P1,P2,...) allows for additional

% arguments which are passed to the function F(X,P1,P2,...).

% Pass an empty matrix for TOL or TRACE to use the default

% value.

%

% See also FZERO, ROOTS, XGX, LI

% (c) by N. Mostoufi & A. Constantinides

% January 1, 1999

% Initialization

if nargin < 3 | isempty(tol)

tol = 1e-6;

end

if nargin < 4 | isempty(trace)

trace = 0;

end

if tol == 0

tol = 1e-6;

end

if (length(x0) > 1) | (~isfinite(x0))

error('Second argument must be a finite scalar.')

end

iter = 0;

fnk = feval(fnctn,x0,varargin{:});

if trace

header = ' Iteration x f(x)';

disp(' ')

disp(header)

fprintf('%5.0d %13.6g %13.6g ',iter, [x0 fnk])

if trace == 2

xpath = [x0 x0];

ypath = [0 fnk];

end

end

x = x0;

x0 = x + 1;

itermax = 100;

% Main iteration loop

while abs(x - x0) > tol & iter <= itermax

iter = iter + 1;

x0 = x;

% Set dx for differentiation

if x ~= 0

dx = x/100;

else

dx = 1/100;

end

% Differentiation

a = x - dx; fa = feval(fnctn,a,varargin{:});

b = x + dx; fb = feval(fnctn,b,varargin{:});

df = (fb - fa)/(b - a);

% Next approximation of the root

if df == 0

x = x0 + max(abs(dx),1.1*tol);

else

x = x0 - fnk/df;

end

fnk = feval(fnctn,x,varargin{:});

% Show the results of calculation

if trace

fprintf('%5.0d %13.6g %13.6g ',iter, [x fnk])

if trace == 2

xpath = [xpath x x];

ypath = [ypath 0 fnk];

end

end

end

if trace == 2

% Plot the function and path to the root

xmin = min(xpath);

xmax = max(xpath);

dx = xmax - xmin;

xi = xmin - dx/10;

xf = xmax + dx/10;

yc = [];

for xc = xi : (xf - xi)/99 : xf

yc = [yc feval(fnctn,xc,varargin{:})];

end

xc = linspace(xi,xf,100);

ax = linspace(0,0,100);

plot(xc,yc,xpath,ypath,xc,ax,xpath(1),ypath(2),'*',x,fnk,'o')

axis([xi xf min(yc) max(yc)])

xlabel('x')

ylabel('f(x)')

title('Newton-Raphson : The function and path to the root

(* : initial guess ; o : root)')

end

if iter >= itermax

disp('Warning : Maximum iterations reached.')

end


2) Example1_1.m

% Example1_1.m

% This program solves the problem posed in Example 1.1.

% It calculates the friction factor from the Colebrook equation

% using the Successive Substitution, the Linear Interpolation,

% and the Newton-Raphson methods.

clear

clc

clf

disp('Calculating the friction factor from the Colebrook equation')

% Input

Re = input(' Reynolds No. = ');

e_over_D = input(' Relative roughness = ');

method = 1;

while method

fprintf(' ')

disp(' 1 ) Successive substitution')

disp(' 2 ) Linear Interpolation')

disp(' 3 ) Newton Raphson')

disp(' 0 ) Exit')

method = input(' Choose the method of solution : ');

if method

fname = input(' Function containing the Colebrook equation : ');

end

switch method

case 1 % Successive substitution

x0 = input(' Starting value = ');

f = xgx(fname,x0,[],2,Re,e_over_D);

fprintf(' f = %6.4f ',f)

case 2 % Linear interpolation

x1 = input(' First starting value = ');

x2 = input(' Second starting value = ');

f = LI(fname,x1,x2,[],2,Re,e_over_D);

fprintf(' f = %6.4f ',f)

case 3 % Newton-Raphson

x0 = input(' Starting value = ');

f = NR(fname,x0,[],2,Re,e_over_D);

fprintf(' f = %6.4f ',f)

end


3) Colebrook.m

function y = Colebrook(f, Re, e)

% Colebrook.m

% This function evaluates the value of Colebrook equation to be

% solved by the linear interpolation or the Newton-Raphson method.

y = 1/sqrt(f) + 0.86*log(e/3.7 + 2.51/Re/sqrt(f));

Explanation / Answer

Yes you can use MATLAB code on scilab. See these links for more information: 1.http://help.scilab.org/docs/5.4.0/fr_FR/section_36184e52ee88ad558380be4e92d3de21.html

2.http://help.scilab.org/docs/5.4.0/en_US/index.html