or

Six Steps to MATLAB Happiness

[Back to main page] [Back to "How do I ..."]

Scripts are the simplest way to write programs in MATLAB. Though they have limitations, they are easy to write and easy to extend. The presentation here is meant to help you start writing script Mfiles in MATLAB. Feel free to extend, alter or otherwise improve on this prescription.

Steps in the process:

- Understand the problem.
- Create an Mfile and initialize the workspace.
- Set givens for the problem.
- Implement the algorithm.
- Produce the requested (or desired) output.
- Is there more?

[Back to main page] [Back to "How do I ..."]

**Step 0**: Read and understand the problem.

The computer is an excellent calculation tool but it does not know what to do to solve problems until you direct it. It is very unusual for someone to just sit down and get results. Some level of preparation and thought is required before using the computer.

Some questions to answer before you start:

- What are the givens/parameters/inputs for the problem?
- What are the finds/results/outputs for the problem?
- What is the algorithm that connects the outputs to the inputs?
- What form should the results/outputs be provided in?

**Step 1**: Create an Mfile and initialize the workspace.

You've decided how to solve it and (importantly) that the computer is an effective means to implementing the solution. If the solution is more than a few lines of MATLAB code, using just the command line is not going to be very efficient. You'll need to create a script that allows you to develop the code for the algorithm. In addition, the script will record your work, and make it plain what your problem-solving strategy is. Here are some things you should do to ensure a reliable set-up of a MATLAB script

- Use
as the first line in the script - always!`clear` - Use
to get rid of any open figures and make it clear what is created by your script.`close all` - If you want to keep open figures, use
instead of`figure`.`close all` - In advanced settings, defining the search path may be important.

- Use
**Step 2**: Set givens for the problem.

Set up what you know about the problem, remembering that your algorithm should determine the results based on the "givens" alone. If that is not possible, re- evaluate your algorithm.

Some items that might be set before any calculations are carried out.

- Values of physical constants (the gas constant, Avogadro's number, the speed of light).
- Parameters for the problem. Use
to ask for parameters you would like to be able to easily vary for your calculations.`input` - Read in or load data or parameter files.

**Step 3**: Implement the algorithm.

MATLAB is ready, you are ready. Time to turn your algorithm (or solution procedure) into hard code. Beware: this is not likely to be a one-shot, linear process. You will have to try things, make errors, correct errors and perhaps even modify your algorithm.

*Give yourself time and patience.*Some issues to remember when coding in MATLAB

- Scripts are sequential sequences of commands, assignments and functions.
- In an assignment (
), everything on the right-hand side must be defined by prior steps.`lhs = rhs` - Make sure all the "finds" are calculated at some point in the algorithm.
- Intermediate results (plots, screen displays, writes to files) may be useful for "sanity" checking.
- Your algorithm may have requests for action
at certain points, so using
may be helpful.`input` - Don't forget to use comments in your code to explain how you did something, especially if a particular step or portion is a key part of your solution (i.e., one that you spent a lot of time on).

**Step 4**: Produce the requested (or desired) output.

The point of solving a problem is to get some results that will give you some insight into the issues you are working on. A typical reason for using the computer is to use the graphical capabilities of the computer to produce plots or other graphic elements. Results should be displayed in meaningful and informative ways to maximize the value of your effort.

Computer output and results can take many forms:

- Plots of functions or data (data or scientific visualization).
- Sending tabulated or calculated results to files, the screen or other programs.
- Sometimes, the Mfile itself is an important result because it clearly documents the method of solution.
- Remember to completely annotate your results by using good table headings and informative axis labels and titles.

**Step 5**: Is there more?

In solving a problem, new ideas and problems come up. Some of them can be very interesting and creative. Some can help you develop a better understanding of your problem. Some can help you improve your problem-solution strategy. Evaluate your results, consider them and maybe ask a "What if?" question.

[Back to main page] [Back to "How do I ..."]

Comments? Contact Jim Maneval at