3 min read

How do I solve equations in MATLAB?

How do I solve equations in MATLAB?

If you're like me, you one day awoke to discover that you were expected to know how to use MATLAB despite having never used it before.

In my case, I was a transfer student that took a C++ programming class at one school that counted as "MATLAB and C++ Programming" at my new school. On my first day of Circuit Analysis class, my professor told us that MATLAB and Pspice counted 10% of our grade.

Needless to say, I spent a lot of time trying to figure out what was going on. If you're reading this post you're probably panicking over an assignment that's due in 4 hours and you can't get this program to work. I'm going to post short segments on each major concept required to do whatever it is you need to do in MATLAB in a conversational format.

This is the book I used for learning MATLAB. If you're going to use MATLAB for anything beyond the basics, definitely pick this book up. It's way cheaper than the textbooks and still covers up to differential equations and transforms.

First things first. How do I use this thing like a calculator?
Just type in what you want to do.
>> 5+1
ans = 6

Notice that a variable named "ans" was created and now has your answer stored in it. If you're not familiar with variables, think of them as little pockets of memory where data is stored. You can create a variable by just typing "name = value" where name is your variable and value is your, well, value.

Let's say you want to solve for the square of a number named "y" and you want to store that in a variable named "x":

>> y = 2;
>>x = y^2

What did that semi-colon do? It suppressed the terminal echo. What??? It didn't repeat anything back to your command line. This is useful for complicated scripts where you don't want your screen flooded with calculations.
Speaking of scripts ... MATLAB allows recursion so you can type

>> 5+1
ans = 6
>> ans = ans + 1
ans = 7

This can be useful any time you need to write scripts and need a counter for your loop statement. That's a topic for another day though.
Let's talk about more operators.

Do you want to do trigonometry?

Just use sin(value) to take the sine of "value." Just realize that MATLAB defaults to radians so if you want to use angles, you need to say so.

For trigonometric functions, you add a "d" to the operator.

>> sind(45)
ans = 0.7071

Let's check by doing a square root of the sine of a 45 degree angle which corresponds to the square root of 2 divided by 2:

>> sqrt(2)/2
ans = 0.7071

Looks good to me. Naturally, your trigonometric operators are sin(), cos(), tan(), sec(), csc(), and cot(). You also have inverse trigonometric operators by just adding an "a" to the mix such as by saying "asin()"
Ok, let's say that we want to use logarithms. You just use log(value) to do a natural logarithm.

Want to do a log for a different base other than Euler's constant?

Just type log_(value) and fill in the blank for the base you want.

Base 10?



What if you want a different numerical format?

Use the format command. There are various formats available such as rat (displays everything as a rational number), bank (displays to two decimal places), long (displays as a long variable, a programming term), short (another programming term), hex (hexadecimal mode). To see the formats available in your version of MATLAB, type "help format."

Check out further tutorials about more advanced operations such as graphing and scripts.

MATLAB is a copyrighted of The MathWorks, Inc. Purchase MATLAB here.
Wirebiters.com and The Math Works, Inc are not affiliated in any way.