Part 1

Getting Started With MATLAB

IN THIS PART …

Considering what MATLAB can do for you

Getting MATLAB installed and ready for use

Working with the MATLAB interface

Interacting with the MATLAB files

Chapter 1

Introducing MATLAB and Its Many Uses

IN THIS CHAPTER

check Understanding how MATLAB fits in as a tool for performing math tasks

check Seeing where MATLAB is used today

check Discovering how to get the most from MATLAB

check Overcoming the MATLAB learning curve

Math is the basis of all our science and even some of our art. In fact, math itself can be an art form — consider the beauty of fractals (a visual presentation of a specialized equation). However, math is also abstract and can be quite difficult and complex to understand and to use for practical purposes. MATLAB makes performing math-related tasks easier. You use MATLAB to perform math-related tasks such as

· Numerical computation

· Visualization

· Scripting

· Application development

· Machine learning

· Deep learning

· Signal processing

· Other tasks allowed by its various toolboxes (see https://www.mathworks.com/help/thingspeak/matlab-toolbox-access.html for details)

This chapter introduces you to MATLAB, an application that performs a variety of math tasks. It helps you understand the role that MATLAB can play in reducing the overall complexity of math and in explaining math-related information to others more easily. You also discover that many organizations and major developers use MATLAB to perform real-world tasks in a manner that improves accuracy, efficiency, and consistency. (A partial list of such tasks appears at https://stackshare.io/matlab.) Of course, knowing how you can translate these benefits of MATLAB to your own workplace is important.

Remember Because MATLAB can do so much, it does have a learning curve. This chapter also discusses what you can do to reduce the learning curve so that you become productive much faster. The less time you spend learning about MATLAB, the more time you spend applying math to your particular specialty, and the better the results you achieve. Getting things done quickly and accurately is the overall goal of MATLAB.

Putting MATLAB in Its Place

MATLAB is all about math. Yes, it’s a powerful tool and yes, it includes its own language to make the execution of math-related tasks faster, easier, and more consistent. However, when you get right down to it, the focus of MATLAB is the math. For example, you could type 2 + 2 as an equation, and MATLAB would dutifully report the sum of 4 as output. Of course, no one would buy an application to compute 2 + 2 — you could easily do that with a calculator. So you need to understand just what MATLAB can do. The following sections help you put MATLAB into perspective so that you can better understand how you can use it to perform complex math tasks.

Understanding how MATLAB relates to a Turing machine

Today’s computers are mostly Turing machines, named after the British mathematician Alan Turing (1912–1954). The main emphasis of a Turing machine is performing tasks step by step. A single processor performs one step at a time. It may work on multiple tasks, but only a single step of a specific task is performed at any given time. Knowing about the Turing machine orientation of computers is important because MATLAB follows precisely the same strategy. It, too, performs tasks one step at a time in a procedural fashion. In fact, you can download an application that simulates a Turing machine using MATLAB at https://www.mathworks.com/matlabcentral/fileexchange/23006-turing-machine-emulator. The code is surprisingly short. (Note that the actual design of a computer relies on principles defined by John von Neumann; see https://cacm.acm.org/magazines/2020/1/241712-von-neumann-thought-turings-universal-machine-was-simple-and-neat/fulltext for details.)

UNDERSTANDING HOW COMPUTERS WORK

Many older programmers are geeks who punched cards before TVs had transistors. One advantage of punching cards is getting to physically touch and feel the computer’s instructions and data. This physicality gave programmers a good understanding of what happens when a program runs.

Today, the instructions and data are stored as charges of electrons in tiny pieces of silicon too small to be seen through even the most powerful optical microscope. Today’s computers can handle much more information much more quickly than early machines. But the way they use that information is basically the same as early computers.

In those old card decks, programmers wrote one instruction on each card. After completing all the instructions, they put the data cards into a card reader. The computer read a card, and the computer did what the card told it to do: Get some data, get more data, add it together, divide, and so on until all the instructions were executed.

A series of instructions is a program. The following figure shows a basic schematic block diagram of how a computer works.

Schematic illustration of a basic block diagram of how a computer works.

Unchanged from the old days, when cards were read one at a time, computer instructions continue to be read one at a time at a low level (think machine code). The instruction is executed, and then the computer goes to the next instruction. MATLAB executes programs in this manner as well.

Don’t confuse the one-instruction-after-another approach with parallel programming. A parallel program relies on individual processors working in tandem to make application execution faster, but the instructions still execute one at a time on each processor. You can employ parallel programming in MATLAB using the Parallel Computing Toolbox, described at https://www.mathworks.com/products/parallel-computing.html.

It’s important to realize that the flow of a program can change. Computers can make decisions based on a specific criterion (based on one comparison even when an expression contains multiple comparisons), such as whether something is true or false, and take the route indicated for that decision. For example, when the computer has read all the data for a task, the program tells the computer to quit reading data and start doing calculations. One way to map how the computer executes programs is called a flow chart, which is similar to a road map with intersections where decisions must be made. MATLAB relies on well-designed flow charts to make it easy to see what the computer will do, when it will do it, and how it will accomplish the required tasks.

The whole concept of a program may seem foreign to many — something that only geeks would ever love — but you’ve already used the concept of a program before. When using a calculator, you first think of the steps and numbers you want to enter and in what sequence to enter them to solve your problem. A program, including a MATLAB program, is simply a sequence of similar steps stored in a file that the computer reads and executes one at a time. You don’t need to fear computer programming — you’ve probably done something very similar quite often and can easily do it again.

Remember Don’t confuse the underlying computer, which relies on machine code, with the high-level programming languages used to create applications for it. Even though the programs that drive the computer may be designed to give the illusion of some other technique, called a programming paradigm, when you look at how the computer works at a low level, you see that it goes step by step. (In fact, some languages support multiple programming paradigms, as described at https://blog.newrelic.com/engineering/python-programming-styles/.) If you’ve never learned how computers run programs, this information serves as meaningful background. Refer to the nearby sidebar “Understanding how computers work” for a discussion of this important background information.

Using MATLAB as more than a calculator

MATLAB is a computer programming language, not merely a calculator. However, you can use it like a calculator, and doing so is a good technique to try ideas that you might use in your program. When you get past the experimentation stage, though, you usually rely on MATLAB to create a program that helps you perform tasks

· Consistently

· Easily

· Quickly

With these three characteristics in mind, the following sections explore the idea of MATLAB’s being more than a simple calculator in greater detail. These sections don’t tell you everything MATLAB can do, but they do provide you with ideas that you can pursue and use to your own advantage.

Exploring Science, Technology, Engineering, and Mathematics (STEM)

Schools currently have a strong emphasis on Science, Technology, Engineering, and Math (STEM) topics because the world doesn’t have enough people who understand these disciplines to get the required work done. Innovation of any sort requires these disciplines, as do many practical trades. MATLAB provides strong support for educational institutions supporting STEM, as described at https://www.mathworks.com/academia/educators.html.

Tip Just in case you’re wondering about the art you can create using MATLAB, consider the use of fractals in venues like cartoons and movies (https://www.sciencenewsforstudents.org/article/math-movies-doctor-strange-otherworldly). Fortunately, you don’t even have to write your own fractal code, because other people have created fractal generators for MATLAB (see https://www.mathworks.com/matlabcentral/fileexchange/78179-the-generator-of-fractal-surfaces-or-images as an example).

Performing simple tasks

The focus of programming languages today is to make things as simple as possible without loss of functionality. This is the reason that articles like https://opensource.com/education/15/9/python-in-the-classroom promote languages like Python, which offer a variety of coding paradigms, yet also provide significant coding power. However, the problem with using a programming language is that you turn into a software developer, impeding your ability to get your research done. When working with MATLAB, you focus on the math, and the interactive environment makes math-specific tasks incredibly easy. In addition, should you need to make your research available to others in a form that allows experimentation, you can design and package your efforts as an application (as described in Chapters 14 and 15) without spending a huge amount of time doing so.

Determining why you need MATLAB

It’s important to know how to use any application you adopt, but it’s equally important to know when to use it and what it can actually do for your organization. If you don’t have a strong reason to use an application, the purchase will eventually sit on the shelf collecting dust. This bit of dust collecting happens far too often in corporations around the world today because people don’t have a clear idea of why they even need a particular application. Given that MATLAB can perform so many tasks, you don’t want it to just sit on the shelf. The following sections can help you build a case for buying and then using MATLAB in your organization.

Using a product that focuses on math optimization

When you buy a programming language, you get tools that help you create great applications. These tools focus on writing better code, which is nice if you’re a developer. However, if you’re a researcher, math professional, or someone who is definitely not interested in becoming a developer, you need a tool that helps you write better algorithms, which is where MATLAB excels. As your algorithms increase in complexity, you can use the MATLAB Profiler, as described at https://www.versionbay.com/articles/2020/matlab-profiler-improving-matlab-code-fibonacci-example/ and in the “Using the MATLAB Profiler to Improve Performance” section of Chapter 8 to improve algorithm performance. The MATLAB Profiler helps you locate precisely where the algorithm bottleneck is so that you can improve your math skills.

Performing what-if analysis quickly

Various kinds of research science require fast answers to what-if analysis. In order to test hypothesis quickly, you need an application that supports quick mock-ups of complex scenarios without a lot of development time. Here are some ways professionals use what-if analysis:

· Medical research professionals use MATLAB to perform visual forms of what-if analysis by relying on the Image Processing Toolbox (https://www.mathworks.com/products/image.html)

· You also see MATLAB used for financial analysis with the Risk Modeling and Risk Management Toolbox (https://www.mathworks.com/help/risk/modeling-risk.html)

· Electronics designers perform circuit analysis (https://www.mathworks.com/academia/books/solving-dc-and-ac-circuits-by-example-using-matlab-haskell.html)

Remember The point is that none of these professionals have time to be a developer if they want to solve problems quickly, which is why they need MATLAB. You can find other examples of professionals who use MATLAB to speed things up in applications like embedded systems, control systems, wireless communication, computer vision, Internet of Things (IoT), testing and measurement, robotics, data analytics, predictive maintenance, power and motor control, and deep learning. Part 5 of the book looks at some key application uses of MATLAB.

Avoiding the complexity of Object-Oriented Programming (OOP)

You may have heard of Object-Oriented Programming (OOP). It’s a discipline that helps developers create applications based on real-world models. Every element of an application becomes an object that has specific characteristics and can perform specific tasks. This technology is quite useful to developers because it helps them create extremely complex applications with fewer errors and less coding time.

However, OOP isn’t something you need to know in order to work through various types of math problems. Even though you can solve difficult math problems using languages that do support OOP, STEM users can exploit most of MATLAB’s power without OOP. The lack of an OOP requirement means that you can get up and running with MATLAB far faster than you could with a conventional modern programming language and without a loss of the functionality that you need to perform math tasks. If you really do want to use OOP with MATLAB, you can find it explained in Chapter 13.

Using the powerful toolbox

MATLAB provides the Symbolic Math toolbox designed to meet the specific needs of STEM users. In contrast to a general programming language, this toolbox provides specific functionality needed to meet certain STEM objectives. Here is just a small sample of the areas that are addressed by the tools you find in the MATLAB Symbolic Math toolbox:

· Calculus

· Equation solving, simplification, and substitution

· Linear algebra

· Visualization

· Variable-precision arithmetic

· Units and dimensional analysis

· Documentation and sharing

· Code generation

Chapters 19 and 20 tell you more about the Symbolic Math toolbox and describe how to perform a variety of tasks with it. You can also see this toolbox described at https://www.mathworks.com/products/symbolic.html.

Reducing programming effort with the fourth-generation language

Programming languages are often rated by their generation. For example, a first-generation language works side by side with the hardware. It’s the sort of language that programmers used when computers first appeared on the scene. Nothing is wrong with working directly with the hardware, but you need specialized knowledge to do it, and writing such code is time consuming. A first-generation language is so hard to use that even the developers decided to create something better — second-generation languages! (Second-generation languages, such as Macro Assembler [MASM] are somewhat human-readable, must be assembled into executable code before use, and are still specific to a particular processor.)

Technical Stuff Most developers today use a combination of third-generation languages such as C, C++, and Java, and fourth-generation languages such as Structured Query Language (SQL) and Python. A few even use fifth-generation languages, such as OPS5 and Mercury. A third-generation language gives the developer the kind of precise control needed to write exceptionally fast applications that can perform a wide array of tasks. Fourth-generation languages make asking for information easier. For the MATLAB user, the promise of fourth-generation languages means being able to work with collections of data, rather than individual bits and bytes, making it easier for you to focus on the task instead of the language. Fifth-generation languages rely on constraints issued to the application and then rely on the application to write the correct code. These languages are used so seldom today that none of them appear on the Tiobe index (see https://www.tiobe.com/tiobe-index/), which is a sort of language hall of fame.

Remember MATLAB employs a fourth-generation language to make your job a lot easier. The language isn’t quite human, but it’s also a long way away from the machine code that developers formerly wrote to make computers work. Using MATLAB makes you more efficient because the language is specifically designed to meet the needs of STEM users (just as SQL is designed to meet the needs of database administrators and developers who need to access large databases).

Discovering Who Uses MATLAB for Real-World Tasks

An application isn’t very useful if you can’t perform real-world tasks with it. Many applications are curiosities — they may do something interesting, but they aren’t practical. MATLAB is popular among STEM users whose main goal is to productively solve problems in their particular field — not problems unique to computer programming. You can find MATLAB used by the following professionals:

· Scientists

· Engineers

· Mathematicians

· Students

· Teachers

· Professors

· Statisticians

· Control technologists

· Image-processing researchers

· Simulation users

Of course, most people want to hear about actual users who employ the product to do something useful. You can find such a list at https://www.mathworks.com/company/user_stories/product.html. Just click the MATLAB entry to see a list of companies that use MATLAB to perform real-world tasks. For example, this list tells you that Bigfoot Biomedical uses MATLAB for diabetes management (see https://www.mathworks.com/company/user_stories/accelerating-development-of-a-diabetes-management-system-with-model-based-design-qa-with-bigfoot-biomedical.html). You also find that 3T (an engineering firm) used MATLAB to design robot emergency braking, which you can read about at https://www.mathworks.com/company/user_stories/3t-develops-robot-emergency-braking-system-with-model-based-design.html.

Knowing How to Get the Most from MATLAB

At this point, you may have decided that you absolutely can’t get by without obtaining your own personal copy of MATLAB. If that’s the case, you really do need to know a little more about it in order to get the most value for your money. The following sections provide a brief overview of the skills that are helpful when working with MATLAB. You don’t need these skills to perform every task, but they all come in handy for reducing the overall learning curve and making your MATLAB usage experience nicer.

Getting the basic computer skills

Most complex applications require that you have basic computer skills, such as knowing how to use your mouse, work with menu systems, understand what a dialog box is all about, and perform some basic configuration tasks. MATLAB works like other computer programs you own. It has the intuitive and conventional Graphical User Interface (GUI), shown in Figure 1-1, that makes using MATLAB a lot easier than employing pad and pen. If you’ve learned to use a GUI operating system such as Windows or macOS, and you also know how to use an application such as Word or Excel, you’ll be fine.

Snapshot of the MATLAB GUI makes performing math tasks easier.

FIGURE 1-1: The MATLAB GUI makes performing math tasks easier.

Tip This book points out MATLAB peculiarities. In addition, you have access to procedures that you can use to make your tasks easier to perform. The combination of these materials will help you work with MATLAB even if your computer skills aren’t as finely honed as they could be. The important thing to remember is that you can’t break anything when working with MATLAB. In fact, I encourage trial and error because it’s a great learning tool. If you find that an example doesn’t quite work as anticipated, close MATLAB, reopen it, and start the example over again. MATLAB and your computer are both more forgiving than others may have led you to believe.

Defining the math requirements

You need to have the right level of knowledge to use MATLAB. Just as using SQL is nearly impossible without any knowledge of database management, using MATLAB is hard without the proper math knowledge. MATLAB’s benefits become evident when applied to trigonometry, exponentials, logarithms, and higher math.

Tip This book assumes that you have a certain level of math knowledge. The math behind the exercises isn’t explained to any large degree unless the explanation helps you understand the MATLAB programming language better. However, many sites online cater to math knowledge. For example, you can find a host of tutorials at https://www.analyzemath.com/ and explanations at Wolfram MathWorld (https://mathworld.wolfram.com) in a classroom-like format. These tutorials come complete with exercises that help you understand the math behind the MATLAB examples in this book.

Applying what you know about other procedural languages

One of the more significant problems in understanding how to use any language is the procedure. The point was driven home to one fellow at an early age when his teacher assigned his class the task of writing a procedure for making toast. Every student carefully developed a procedure for making toast, and on the day the papers were turned in, the teacher turned up with a loaf of bread and a toaster. She dutifully followed to the letter the instructions each child provided. All the children failed at the same point. Yes, they forgot to take the bread out of the wrapper. You can imagine what it was like trying to shove a single piece of bread into the toaster when the piece was still in the wrapper along with the rest of the bread.

Programming can be (at times) just like the experiment with the toast. The computer takes you at your word and follows to the letter the instructions you provide. The results may be not what you expected, but the computer always follows the same logical course. Having previous knowledge of a procedural language, such as C, Java, C++, or Python, will help you understand how to write MATLAB procedures as well. You have already developed the skill required to break instructions into small pieces, and you know what to do when a particular piece is missing. Yes, you can use this book without any prior programming experience, but the prior experience will help you get through the chapters faster and with fewer errors.

Understanding how this book will help you

This is a For Dummies book, so it takes you by the hand to explore MATLAB and make it as easy to understand as possible. The goal of this book is to help you use MATLAB to perform at least simple feats of mathematical magic. It won’t make you a mathematician, and it won’t help you become a developer — those are topics for other books. When you finish this book, you will know how to use MATLAB to explore STEM-related topics.

Getting Over the Learning Curve

Even easy programming languages have a learning curve. If nothing else, you need to discover the techniques that developers use to break tasks into small pieces, ensure that all the pieces are actually there, and then place the pieces in a logical order. Creating an orderly flow of steps that the computer can follow can be difficult, but this book leads you through the process a step at a time.

To help you understand MATLAB, this book compares how to accomplish a task in MATLAB with something you’re used to using, such as a spreadsheet or calculator. You learn by doing. Try the examples in this book and invent some of your own. Try variations. Experiment. MATLAB’s not too tough — you, too, can discover how to use MATLAB!

If you find an error or have any questions, please email us at admin@erenow.org. Thank you!