Matlab Function

  1. Matlab Function Cat
  2. Matlab Function File
  3. Matlab Function Dot
  • Matlab Tutorial

The solve function is used for solving algebraic equations. In its simplest form, the solve function takes the equation enclosed in quotes as an argument. For example, let us solve for x in the equation x-5 = 0. Solve ('x-5=0') MATLAB will execute the above statement and return the following result −. Local functions in the current file have precedence over functions in other files. That is, when you call a function within a program file, MATLAB checks whether the function is a local function before looking for other main functions.

  • MATLAB Advanced
  • MATLAB Useful Resources
  • Selected Reading

Matlab Function Cat

So far, we have seen that all the examples work in MATLAB as well as its GNU, alternatively called Octave. But for solving basic algebraic equations, both MATLAB and Octave are little different, so we will try to cover MATLAB and Octave in separate sections.

We will also discuss factorizing and simplification of algebraic expressions.

Solving Basic Algebraic Equations in MATLAB

The solve function is used for solving algebraic equations. In its simplest form, the solve function takes the equation enclosed in quotes as an argument.

For example, let us solve for x in the equation x-5 = 0

MATLAB will execute the above statement and return the following result −

You can also call the solve function as −

MATLAB will execute the above statement and return the following result −

You may even not include the right hand side of the equation −

MATLAB will execute the above statement and return the following result −

If the equation involves multiple symbols, then MATLAB by default assumes that you are solving for x, however, the solve function has another form −

where, you can also mention the variable.

For example, let us solve the equation v – u – 3t2 = 0, for v. In this case, we should write −

MATLAB will execute the above statement and return the following result −

Solving Basic Algebraic Equations in Octave

The roots function is used for solving algebraic equations in Octave and you can write above examples as follows −

For example, let us solve for x in the equation x-5 = 0

Octave will execute the above statement and return the following result −

You can also call the solve function as −

Octave will execute the above statement and return the following result −

Solving Quadratic Equations in MATLAB

The solve function can also solve higher order equations. It is often used to solve quadratic equations. The function returns the roots of the equation in an array.

The following example solves the quadratic equation x2 -7x +12 = 0. Create a script file and type the following code −

When you run the file, it displays the following result −

Matlab Function File

Solving Quadratic Equations in Octave

The following example solves the quadratic equation x2 -7x +12 = 0 in Octave. Create a script file and type the following code −

When you run the file, it displays the following result −

Solving Higher Order Equations in MATLAB

The solve function can also solve higher order equations. For example, let us solve a cubic equation as (x-3)2(x-7) = 0

MATLAB will execute the above statement and return the following result −

In case of higher order equations, roots are long containing many terms. You can get the numerical value of such roots by converting them to double. The following example solves the fourth order equation x4 − 7x3 + 3x2 − 5x + 9 = 0.

Create a script file and type the following code −

When you run the file, it returns the following result −

Please note that the last two roots are complex numbers.

Solving Higher Order Equations in Octave

The following example solves the fourth order equation x4 − 7x3 + 3x2 − 5x + 9 = 0.

Create a script file and type the following code −

When you run the file, it returns the following result −

Solving System of Equations in MATLAB

The solve function can also be used to generate solutions of systems of equations involving more than one variables. Let us take up a simple example to demonstrate this use.

Let us solve the equations −

5x + 9y = 5

3x – 6y = 4

Create a script file and type the following code −

When you run the file, it displays the following result −

In same way, you can solve larger linear systems. Consider the following set of equations −

x + 3y -2z = 5

3x + 5y + 6z = 7

2x + 4y + 3z = 8

Solving System of Equations in Octave

We have a little different approach to solve a system of 'n' linear equations in 'n' unknowns. Let us take up a simple example to demonstrate this use.

Let us solve the equations −

5x + 9y = 5

3x – 6y = 4

Such a system of linear equations can be written as the single matrix equation Ax = b, where A is the coefficient matrix, b is the column vector containing the right-hand side of the linear equations and x is the column vector representing the solution as shown in the below program −

Create a script file and type the following code −

When you run the file, it displays the following result −

In same way, you can solve larger linear systems as given below −

x + 3y -2z = 5

3x + 5y + 6z = 7

2x + 4y + 3z = 8

Expanding and Collecting Equations in MATLAB

The expand and the collect function expands and collects an equation respectively. The following example demonstrates the concepts −

When you work with many symbolic functions, you should declare that your variables are symbolic.

Create a script file and type the following code −

When you run the file, it displays the following result −

Expanding and Collecting Equations in Octave

Matlab

You need to have symbolic package, which provides expand and the collect function to expand and collect an equation, respectively. The following example demonstrates the concepts −

When you work with many symbolic functions, you should declare that your variables are symbolic but Octave has different approach to define symbolic variables. Notice the use of Sin and Cos, which are also defined in symbolic package.

Create a script file and type the following code −

When you run the file, it displays the following result −

Factorization and Simplification of Algebraic Expressions

The factor function factorizes an expression and the simplify function simplifies an expression. The following example demonstrates the concept −

Matlab Function Dot

Example

Create a script file and type the following code −

When you run the file, it displays the following result −


Matlab® provides very useful tools to calculate different mathematical operations. One of them is for the calculation of the polygamma functions. ‘Polygamma functions’ is a very important aspect of advanced mathematics. You can find detailed information about the polygamma functions from this link.

YOU CAN LEARN MatLab® IN MECHANICAL BASE; Click And Start To Learn MatLab®!

Polygamma functions are calculated with the ‘psi()’ command in Matlab®. Here we explained how to calculate polygamma functions in Matlab® with the ‘psi()’ command. You can try this command in your Matlab® software.

How To Use The ‘psi()’ Command In MatLab®?

As you see in the programming example above, you need to enter two inputs inside the ‘psi()’ command in Matlab® to calculate the polygamma function. The only requirement is that the first term must be bigger than 4.

The general expression of the polygamma function is like the above. The first term that you entering inside the brackets is the ‘m’ above. And the second term is the ‘z’. Polygamma function is calculated with these values in Matlab®.

Conclusion

As you understand that the calculation of the polygamma functions in Matlab® is very simple with the ‘psi()’ command.

Do not forget to leave your comments and questions below about the use of the ‘psi()’ command in Matlab® below.

If you want further coding examples about the ‘psi()’ command in Matlab®, inform us in the comments.

This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®

Comments are closed.