Using a Function (MATLAB)

If we need to run the same algorithms or formula for computations again and again, then instead of writing the same stuffs in our script file many times, we can simply write a function. This makes our script look simpler and faster to run.

Writing a function in MATLAB


Calculating mean and standard deviation

Calculating distance from origin

Calculates distance (in km) between two coordinate points on the Earth’s surface

—Utpal Kumar (IES,  Academia Sinica)

Published by


For any comments and suggestions, write to me at:

3 thoughts on “Using a Function (MATLAB)

  1. This might be very basic. But following the function logic I wrote this:

    phi = 0:1:90
    sin_phi= sind (phi);

    K = (sin_phi/(1-sin_phi))

    Results show only one K. I want all of the K’s for all of the Phi’s.
    May I ask a hint where to go then?



  2. @Rocio: Here, your variable sin_phi is an array. In MATLAB, for division with an array, the operator “./” is used instead of “/”. Here, you need to change K = (sin_phi./(1-sin_phi)) to get an array output.

    Liked by 1 person

    1. Thanks codethedata!
      I’ll will move forward with my exercise then. I skipped continuing with the problem, now I’ll go back to it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s