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)

3 thoughts on “Using a Function (MATLAB)

Add yours

  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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: