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

Examples:

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

utpalkumar

For any comments and suggestions, write to me @ earthinversion@gmail.com

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?

    Thanks!!!

    Like

  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.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.