Find A Real Root of The Equation F (X) X: Problem
Find A Real Root of The Equation F (X) X: Problem
Problem: Find a real root of the equation f(x)=x3-x-1=0 by using bisection method.
function [root,fx,ea,iter]=bisect(func,xl,xu,es,maxit,varargin)
if nargin<3,error('at least 3 input arguments required'),end
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0,error('no sign change'),end
if nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
iter = 0; xr = xl; ea = 100;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr; fx = func(xr, varargin{:});
OUTPUT
On Command Window On Workspace
MATLAB Program No: 02
Program Name: An M-File to implement the Newton Raphson Method.
Problem: Use the Newton-Raphson method to find a root of the equation x3-2x-5=0
function
[root,ea,iter]=newtraph(func,dfunc,xr,es,maxit,varargin)
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;
OUTPUT
On Command Window On Workspace
MATLAB Program No: 03
Program Name: An M-File to implement Linear regression.
Problem: An experiment gave the following table of values for the dependent variable y for
a set of known values of x. Obtain an appropriate least squares fit for the data and plot y vs x
with MATLAB.
x 1 2 3 4 5 6 7 8 9
y 5.5 7.0 9.6 11.5 12.6 14.4 17.6 19.5 20.5
OUTPUT
On Command Window Plot y vs x;
MATLAB Program No: 04
Program Name: An M-File to implement Lagrange Interpolation.
OUTPUT
Problem: Values of x (in degrees) and sin x are given in the following table:
x (in degrees) 15 20 25 30 35 40
sin x 0.258819 0.3420201 0.4226183 0.5 0.5735764 0.6427876
OUTPUT
On Command Window On Workspace
MATLAB Program No:06
Program Name: An M-File to implement the Trapezoidal Rule.
1
1
Problem: Evaluate I= dx correct to three decimal places with h=0.125.
0 1 x
function I = trap(func,a,b,n,varargin)
if nargin<3,error('at least 3 input arguments required'),end
if ~(b>a),error('upper bound must be greater than lower'),end
if nargin<4|isempty(n),n=1000000;end
x = a; h = (b - a)/n;
s=func(a,varargin{:});
for i = 1 : n-1
x = x + h;
s = s + 2*func(x,varargin{:});
end
s = s + func(b,varargin{:});
I = (b - a) * s/(2*n);
OUTPUT
function I = trapuneq(x,y)
% trapuneq: unequal spaced trapezoidal rule quadrature
% I = trapuneq(x,y):
% Applies the trapezoidal rule to determine the integral
% for n data points (x, y) where x and y must be of the
% same length and x must be monotonically ascending
% input:
% x = vector of independent variables
% y = vector of dependent variables
% output:
% I = integral estimate
if nargin<2,error('at least 2 input arguments required'),end
if any(diff(x)<0),error('x not monotonically ascending'),end
n = length(x);
if length(y)~=n,error('x and y must be same length'); end
s = 0;
for k = 1:n-1
s = s + (x(k+l)-x(k))*(y(k)+y(k+l))/2;
end
I = s;
Input program:
x = [0 .12 .22 .32 .36 .4 .44 .54 .64 .7 .8];
y = 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
trapz(x,y)
OUTPUT
On Command Window On Workspace