Matlab Solver
Matlab Solver
Matlab Solver
Roots of Equations
1) fzero This built in 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 e.g. >> fzero (@(x) (equation in x), (initial value)) >> fzero (@(x) (exp(x)*cos(x)-1.4), 0) ans = 0.4336 2) roots This built in function is used to solve the algebraic equations only. But this function gives all possible roots of given algebraic equation. Syntax >> p = [ >> roots (p) e.g. >> p = [1 0 -5 3]; >> roots (p) ans = -2.4909 1.8342 0.6566 As given polynomial is cubic, it is having 3 roots. This is f(X) = X3 + 0*X2 5*X+3 ]; Defining polynomial in MATLAB
Numerical Integration
1) quad This built in function calculates integration of given function by using recursive adaptive Simpsons Quadrature method (This is some advanced method used by MATLAB). Syntax e.g. >> quad (@(x) (function in x), Lower Limit, Upper Limit) >> quad (@(x) (exp(x)+x.^3-2*x+1), 1, 4) ans = 103.6299
Simultaneous Equations
1) linsolve This built in 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 = [ ; ; ; ] ; ] Defining matrix A Defining matrix B Using function The question is solve the following set of simultaneous equations 2x + 4y 6z = 4 2 1 1 >> B = [-4; 10; 5] B= -4 10 5 >> linsolve (A,B) ans = -3 2 1 So, thus we first define matrix A and B then use the function linsolve. [ ] [ ] [ ] 4 5 3 -6 3 2 x+ 5y + 3z = 10 x + 3y + 2z = 5 In matrix form it will be
e.g.
>> A = [2 4 -6; 1 5 3; 1 3 2] A=
Curve Fitting
1) fit This 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).
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 = [ ; ; ; ; ; ; ; ; ; ; ; ; ; ; ] ] Column vectors X and Y are defined first. Then fit function is used.
>> fit (X, Y, Fit Type) Fit Type the nature of curve you want to fit in given data points. poly1 to fit linear curve poly2 to fit quadratic curve exp1 to fit exponential curve power1 to 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 The question is fit a curve Y = a Xb using following data points X Y 2000 3000 4000 5000 6000 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
Coefficients (with 95% confidence bounds): a= b= 2) polyfit This 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 = [ ]; First define X and Y data points. This time X and Y can either be row or column vector. Then use >> Y = [ ]; the function. N is the degree of polynomial that you want to fit. >> 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 The question is Fit 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 4.15 (0.8384, 7.461) 0.1661 (0.06975, 0.2625)