MATLAB essential/How to solve mathematical equations: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Line 22: Line 22:


{{BookCat}}
{{BookCat}}
Open your MATLAB or go to this [http://www.tutorialspoint.com/matlab/try_matlab.php website] if you do not have access to MATLAB.

== Important information to remember ==
== Important information to remember ==



Revision as of 04:34, 22 October 2015

Open your MATLAB or go to this website if you do not have access to MATLAB.

Important information to remember

Before starting to solve any equation in MATLAB, it 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 visit 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:

When you have a variable (symbol)

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 smiley faces used in chatting . For example: If you want to write: f(x)= x^2+5, in MATLAB syntax it is written as:

f=@(x) x^2 + 5

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

when you have a vector:

Sometimes, (x) can have more than one value, then it can be called a vector. Let's consider the same function, but when x is a vector instead of a variable. If we want to express this function in MATLAB, we 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 on below to know how to define these functions.

Limits

Remember those limits that you used to struggle with in high school and Calculus I and II ? 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 the command window or the editor:

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

As noted in the first lecture, it always best to 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)

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

MATLAB and trigonometry

Differentiation and integration

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

Differentiation:

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.

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

What if you had a vector instead of a symbol:

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 three values ( 10, 20, or 30 ), so x can be considered as a vector. In order to find when x has its three 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 messege:

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, I often forget to write this part, but it is very important. After calculating the derivative, you create a new variable that has the same value as df1, we can 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

Integration

With no limits of integration

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:

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

External Links

You can use these links to learn more about this lecture: