matlab solver

Upload: sudhanwa-kulkarni

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Matlab Solver

    1/3

    MATLAB SOLVER

    Roots of Equations

    1) fzeroThis builtin function is used to solve transcendental as well as algebraic equations. But this

    function gives just one root of the equation nearer to the given initial value.

    Syntax >> fzero (@(x) (equation in x), (initial value))

    e.g. >> fzero (@(x) (exp(x)*cos(x)-1.4), 0)

    ans =

    0.4336

    2) rootsThis builtin function is used to solve the algebraic equations only. But this function gives all

    possible roots of given algebraic equation.

    Syntax >> p = [ ]; Defining polynomial in MATLAB

    >> roots (p)

    e.g. >> p = [1 0 -5 3]; This is f(X) = X3

    + 0*X25*X+3

    >> roots (p)

    ans =

    -2.4909

    1.8342

    0.6566

    Numerical Integration

    1) quadThis built in function calculates integration of given function by using recursive adaptiveSimpsons Quadrature method (This is some advanced method used by MATLAB).

    Syntax >> quad (@(x) (function in x), Lower Limit, Upper Limit)

    e.g. >> quad (@(x) (exp(x)+x.^3-2*x+1), 1, 4)

    ans = 103.6299

    As given polynomial is cubic, it is having 3

    roots.

  • 7/27/2019 Matlab Solver

    2/3

    Simultaneous Equations

    1) linsolveThis builtin function solves the given system of simultaneous equation, (A * X = B) by LU

    factorization method (This is some advanced method used by MATLAB; dont go into details of

    it). Initially, define matrix of coefficients i.e. matrix A then define matrix of constants i.e. matrix

    B. Then use this function.

    Syntax >> A = [ ; ; ]

    >> B = [ ; ; ]

    >> linsolve (A, B)

    e.g. >> A = [2 4 -6; 1 5 3; 1 3 2]

    A =

    2 4 -6

    1 5 3

    1 3 2

    >> B = [-4; 10; 5]

    B =

    -4

    10

    5

    >> linsolve (A,B)

    ans =

    -3

    2

    1

    Curve Fitting

    1) fitThis function fits a curve to given data i.e. it gives the values of constants present in the equation

    that describes the curve (e.g. while using this function to fit a straight line to given data, it

    gives values of constants a and b which are present in equation of line Y = a * X + b).

    Defining matrix A

    Defining matrix B

    Using function

    [

    ]

    [

    ]

    The question issolve the following

    set of simultaneous equations

    2x + 4y6z =4

    x+ 5y + 3z = 10

    x + 3y + 2z = 5

    In matrix form it will be

    So, thus we first define matrix A and

    B then use the function linsolve.

  • 7/27/2019 Matlab Solver

    3/3

    Initially, define X and Y vectors i.e. the data points. Note that, these vectors must be column

    vectors. Then use this function.

    Syntax >> X = [ ; ; ; ; ; ; ; ]

    >> Y = [ ; ; ; ; ; ; ; ]

    >> fit (X, Y, Fit Type)

    Fit Typethe nature of curve you want to fit in given data points.

    poly1to fit linear curve

    poly2to fit quadratic curve

    exp1to fit exponential curve

    power1to fit power equation

    e.g. >> X = [2000 3000 4000 5000 6000];>> Y = [15 15.5 16 17 18];

    >> fit (X', Y', 'power1')

    ans =

    General model Power1:

    ans(x) = a*x^b

    Coefficients (with 95% confidence bounds):

    a = 4.15 (0.8384, 7.461)

    b = 0.1661 (0.06975, 0.2625)

    2) polyfitThis function is used to fit polynomial curve i.e. straight line or quadratic curve to the given data

    points. First define X and Y data points as a vector. Then use this function.

    Syntax >> X = [ ];

    >> Y = [ ];

    >> polyfit (X, Y, N)

    e.g. >> X = [-3 -2 -1 0 1 2 3];

    >> Y = [12 4 1 2 7 15 30];

    >> polyfit (X, Y, 2)

    ans =

    2.1190 2.9286 1.6667

    Column vectors X and Y are defined first.

    Then fit function is used.

    The question isfit a curve Y = a X using

    following data points

    X 2000 3000 4000 5000 6000

    Y 15 15.5 16 17 18

    Find the values of a and b.

    Thus, first define X and Y column vectors and

    then use function as mentioned

    First define X and Y data points. This time X and

    Y can either be row or column vector. Then use

    the function. N is the degree of polynomial that

    you want to fit.

    The question isFit a second degree

    polynomial of the type a*X2

    + b*X + c, in the

    following data points

    X -3 -2 -1 0 1 2 3

    Y 12 4 1 2 7 15 30