Course Main Page: MATLAB essential Course Content: Lecture 1 : General information + arrays Lecture 2: MATLAB and mathematical equations Quiz 1: Quiz on arrays and mathematical equations Lecture 3: 2D and 3D plotting Lecture 4: If statements and loops Before solving any equation in MATLAB, it is extremely important to be familiar with these symbols and expressions. 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. 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: f ( x ) = x 2 + 5 {\displaystyle f(x)=x^{2}+5} in MATLAB you type : f=@(x) x^2 + 5 In this case, (x) is a variable, meaning it can only have one value. 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 : f ( x ) = x ∗ s i n ( 5 x ) {\displaystyle f(x)=x*sin(5x)} f ( x ) = e x x {\displaystyle f(x)={\tfrac {e^{x}}{x}}} v ( u , n ) = n + 4 y {\displaystyle v(u,n)={\sqrt {n}}+4y} Look at the picture below to know how to define these functions in MATLAB. 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 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 F ( x ) lim x → ∞ = 1 x + x 2 {\displaystyle F(x)\lim _{x\to \infty }={\tfrac {1}{x}}+x^{2}} 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) Now let's try another problem. For example, F ( x ) lim x →   0 = s i n ( x ) x {\displaystyle F(x)\lim _{x\to \ 0}={\tfrac {sin(x)}{x}}} , 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: F ( x ) lim x →   0 = e x x {\displaystyle F(x)\lim _{x\to \ 0}={\tfrac {e^{x}}{x}}} . limit( exp(x)/x,0) MATLAB answer would be NaN Just like limits, differentiating and integrating using MATLAB is very easy, and all you have to know is the syntax. The key word to remember when trying to differentiate a function is ( diff ). For example, if you want to differentiate w = e 2 t 4 + t 3 + t 9 {\displaystyle w={\tfrac {e^{2}t}{4}}+t^{3}+{\sqrt {t^{9}}}} 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. To know more about partial integration click here, or here. Now assume you have the function: f ( x , y , z ) = 2 x + 3 y 2 + 6 z 3 {\displaystyle f(x,y,z)=2x+3y^{2}+6z^{3}} .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. 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: f ( x ) = x 2 − 3 x {\displaystyle f(x)=x^{2}-{\tfrac {3}{x}}} The variable x here can have two values ( 1 and 2 ), so x can be considered as a vector. In order to find f ′ ( x ) {\displaystyle f'(x)} when x has two numerical values. First, you need to define the function f ( x ) = x 2 − 3 x {\displaystyle f(x)=x^{2}-{\tfrac {3}{x}}} 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): 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. The keyword to keep in mind when integrating using MATLAB is ( int ) that is short for integration. When integrating a function f ( x ) {\displaystyle f(x)} you write : i n t ( f ( x ) ) {\displaystyle int(f(x))} . For example, if you want to integrate the function : f ( x ) = x e x {\displaystyle f(x)=xe^{x}} , 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. 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 y = ∫ 0 π c o s ( x ) + s i n ( x ) d x {\displaystyle y={}\int _{0}^{\pi }cos(x)+sin(x)\,dx} . 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). Do you any suggestions?, Click here to send me an email.
{'wiki_source': 'wikiversity', 'other_info': "{'content_model': 'wikitext', 'language': 'en', 'title': 'MATLAB essential/How to solve mathematical equations', 'type': 'text'}"}