Skip to main content

FundaTip2 : Script File

Simple visual trick to minimize errors in your program. 


We often encounter various errors while programming. Sometime it is very difficult to find, if the error is manual. I would like to share a trick to avoid the programming errors while writing a program in script file or function file.

Consider a simple example of "Finding sum of first 'n' numbers."

Following are the recurring errors we may encounter while programming -

You can check with Top-Right Corner of script/function file. The red color box represents the syntax error. The horizontal line below that makes it easier for us to find error command.

Error 1: Check with correct function file syntax to avoid this error (parenthesis).


Error 2: No other command is allowed before function command.


Error 3: End statement is missing.


Warnings in the script/function file is denoted by orange color box. Warning is a suggestion by MATLAB to make program execute quicker. When warning is shown, the unnecessary output is printed in command window. When you see warning, you are just one step behind to make your program ideal.


Warning: The program will take more time to execute. 


The ideal program will always display the green box at Top-Right Corner. Even if the program shows green box, it can not detect manual errors.


Ideal Program: Program with no warnings and errors. 


Always keep good practice of making your program ideal. A reader can understand the ideal code quickly and easily, without much explanation.


Learn best programming practices and be a pro-programmer with simple and easy tips and tricks.

Click HERE to subscribe to get regular email notifications of MATLAB Tips & Tricks.

Regards.
Rupesh Gosavi. 

Comments

Popular posts from this blog

MATLAB Programs - Numerical Methods and Optimization

Roots of Equation - BiSection Method Bisection method is one of the numerical methods to find roots of the equation, included in the syllabus of Third Year Mechanical Engineering course for colleges under University of Pune. The bisection method in mathematics is a root-finding method that repeatedly bisects an interval and then selects a subinterval in which a root must lie for further processing. It is a very simple and robust method, but it is also relatively slow. Because of this, it is often used to obtain a rough approximation to a solution which is then used as a starting point for more rapidly converging methods. Let us consider an example  Figure 1: Polynomial function in f(x)=0 form. To solve polynomial function, it has to be converted into f(x) = 0 form, where all the terms containing powers of x and constant should be on the left-hand side of the equal to sign. Create a function file in MATLAB as shown in figure 1, having a polynomial equation in...

FundaTip1 : Script File

Why commenting is important in programming? While writing a program in script file, it is always recommended to add comments. Comments will help reader to understand following things  - Project title and details about the same.  Purpose of variables used, in mathematical equations.  Declaration of given data variables.  Units of the given variable and output variable.  Assumptions made while working with mathematical model in your project. Let's take a look at the example below -  A program without comments.  In the example above we are not able to understand what is tC , CwT or rC . Even with oak and pine variables, its difficult to understand why those values? And what is its unit? What is the goal of the program?  But with comments reader can easily understand the purpose of program, goal of a programmer, the units of variable and nomenclature of input variables. A program with comments. A program...

FundaTip3: Script File

Some important functions you should not forget! In mathematical programming environment, it is really necessary for us to know various functions which can perform fundamental operations. I am sharing a curated list of functions which are very much useful while programming mathematical models in MATLAB. Fundamental mathematical functions you should know. Let's take some examples to experience the use of these functions - Commands used in simple mathematical equations.  Variables 'a' , 'b' are defined by user, which are known as independent variables . Variables 'x' and 'y'  are output of some mathematical operation of 'a' and 'b', which is known as dependent variable .  Simple but important mathematical functions used in equations. 'Log' denotes the natural log  and 'Log10'  denotes the log to the base 10. Please do not get confuse with analytical commands or commands in scientific ...