MATLAB essential/How to solve mathematical equations

From Wikiversity
Jump to navigation Jump to search

Open your MATLAB or go to this website if you do not have access to MATLAB on your PC. If you want to use the editor (script on the left) make sure to save your work using the drop-down menu before running it

Important information to keep in mind[edit | edit source]

Before solving any equation in MATLAB, it is extremely important to be familiar with these symbols and expressions.

Symbol MATLAB syntax Name MATLAB documentation
ex exp(x) natural exponential function click her for more information
17 ! factorial(17) factorial click here for more information
π = 3.14 pi pi click here for more information
ln (x) log(x) Natural logarithm click here for more information
log(x) log10(x) Common logarithm click here for more information
sqrt(y) square root click here for more information

To know more about the symbols, you can either click on the function's name to go to its Wikipedia page, or click on the last column to go to its MATLAB documentation.

How to express a function in MATLAB[edit | edit source]

When you have a variable (symbol)[edit | edit source]

In order to express a function in MATLAB language, you need first to write  =@ between the name of the function and its variables. If it helps you remember, think of the =@ symbol as one of the emoji faces used in chatting. For example: If you want to write: in MATLAB you type :

f=@(x) x^2 + 5

In this case, (x) is a variable, meaning it can only have one value.

when you have a vector[edit | edit source]

Sometimes, (x) can have more than one value, then it can be called a vector. Let's consider the same function, but in this case (x) is a vector instead of a variable. If you want to express this function in MATLAB, you code:

f=@(x) x.^2 + 5

The only difference here is the dot before the (^) sign. Let's consider a more compels function, such as :

Look at the picture below to know how to define these functions in MATLAB.

Is the dot necessary when dealing with vectors?[edit | edit source]

To answer this question, consider this example.

syms x; 
y= [ 2, 3]
f=@(x) x^2 - 3/x
f(y)

Output: Error, Inputs must be a scalar and a square matrix.

syms x; 
y= [ 2, 3]
f=@(x) x.^2 - 3./x
f(y)

Output: ans = 2.5000    8.0000

Limits[edit | edit source]

Remember those limits that you used to struggle with in school? Now they can be solved easily using MATLAB. You only have to familiarize yourself with the syntax with can be a bit tricky.
For example, If you want to find the limit for you can write the following in either in the command window or in the editor:

syms x;
limit( 1/x + x^2,inf)

As noted in the first lecture, you must define the variable using the word syms The inf is short for infinity(∞), and the rest of the code is self explanatory.The answer would be given in the command window as (inf)

Horizontal asymptotic about y = 4

Now let's try another problem. For example, , we code:

limit( sin(x)/x,inf)

The answer for this will be one(1)

When you try to find a limit that does not exist, for example: .

limit( exp(x)/x,0)

MATLAB answer would be NaN

You may be interested in: YouTube_MATLAB_Limits AND MathWorks_MATLAB_Limits

Differentiation and integration[edit | edit source]

Just like limits, differentiating and integrating using MATLAB is very easy, and all you have to know is the syntax.

Differentiation[edit | edit source]

The key word to remember when trying to differentiate a function is ( diff ). For example, if you want to differentiate

syms t
w=(exp(2*t))/4 + t^3 + t^(9/2);
diff(w)

The answer will be given in the command window as:

exp(2*t)/2 + 3*t^2 + (9*t^(7/2))/2

What if you want to find the second derivative of w. Then you code:

diff(w,2)

The answer will be given in the command window as :

 6*t + exp(2*t) + (63*t^(5/2))/4

This answer is very complicated. It is possible to ask MATLAB to write it in a neat way by coding:

pretty (w)

The output now will be:

exp(2 t)    3    9/2
-------- + t  + t
    4

You can check the accuracy of the previous two operations by doing the calculation yourself here. Likewise, finding the third derivative, or any other derivative, would be done by typing the number of times you want to differentiate a function in the parenthesis.

The image at the left illustrates an application of differentiation using MATLAB. The following two 3D graphs are perpendicular, meaning one of them is tangent to the other. The equation of the tangent shape was found using differentiation, and the two graphs were plotted using MATLAB.

an application of differentiation
an application of differentiation

Partial differentiation[edit | edit source]

To know more about partial integration click here, or here. Now assume you have the function: .If you want partially differentiate this function with respect to y, you write:

syms x y z
f=@(x,y,z) 2*x +3*y^2+ 6*z^3
dfy = diff( f(x,y,z),y)

The output for that would be

dfy =
 
6*y

Partial differentiation is also used to find tangent surface for a 3D graph. This tangent surface is usually parallel to two axis, meaning it is in 2D, unlike the surface obtained using normal differentiation.Refer to the image to the right.

You may be interested in: YouTube_MATLAB_Differentiation

What if you had a vector instead of a symbol[edit | edit source]

Refer back to the previous lecture if you want to know what are vectors in MATLAB.

We use this method when the variable in a certain function can have more than one value. For example, we have the following equation:

The variable x here can have two values ( 1 and 2 ), so x can be considered as a vector. In order to find when x has two numerical values.

First, you need to define the function by writing:

syms x;      
f=@(x) x.^2 - 3./x      

Make sure to use the dots because here we are dealing with vectors, not variables. After defining the function, we calculate the derivative, by writing;

df= diff(f(x))

When you hit Enter, in the command window, MATLAB will calculate the derivative, and show the following output:

df1 =
2*x + 3/x^2

You have now differentiated the function that contain vector successfully. If you want to give values to the vector (x) you have to do the following:

Now this is the most tricky part. After calculating the derivative, you create a new variable that has the same value as df1, we call this function df

df=@(x) subs(df1); 

After that, you plug in the values of the vector:

df([2,1])

The output would be:

ans =
[ 19/4, 5]

Here is another example. Refer to the image below ( click on it to see it in full size):

Example#2

If you have a long function, it is tedious to put a dot before every mathematical operation. In this case the function [ vectorize ] could be used. For example, if I write in the editor:

vectorize(ans)

The output of the previous code would be vectorised, meaning a dot will be put before every operation. This function can saves a lot of time doing tedious work of vectorizing a function manually.

You might be interested in: MathWorks_MATLAB_differentiation.

Integration[edit | edit source]

With no limits of integration[edit | edit source]

The keyword to keep in mind when integrating using MATLAB is ( int ) that is short for integration. When integrating a function you write : . For example, if you want to integrate the function : , then you will write in the editor:

int (x*exp(2*x)) 

When you hit *ENTER* MATLAB will give you the answer in the command window as:

ans =
 
exp(2*x)/2

Do not forget to define a function.

With limits of integration[edit | edit source]

Integration with limits is very similar to integrating without limits. When you have limits for integration, you have to write these limits within the parenthesis that follow the keyword ( int ) . For example: find the value of . To find the answer, you code:

syms x; 
int(cos(x) + sin(x),0, pi)

After hitting *ENTER* the answer in the command window will be:

ans =
 
2

Integration could be used to find the area under a certain curve (Look at the image to the right).


You might be interested in: YouTube_MATLAB_integration. AND MathWorks_MATLAB_Integration


Do you any suggestions?, Click here to send me an email.