Function generation in Octave from scratch part 1

in #utopian-io7 years ago (edited)

What Will I Learn?

Greetings, in this tutorial we will focus on making functions in Octave by doing beginner, scratch level tasks.

  • You will learn Octave's panels and it's properties,
  • You will learn function writing in Octave,
  • You will learn transfering your problem into function,
  • You will learn execution and debugging of a function via command window.

Requirements

  • Octave 4.2
  • Basic knowledge on coding
  • Basic knowledge on sequences

Difficulty

  • Basic

Tutorial Contents

The very first function we will generate will be related with analytic geometry. For this task it was asked to calculate the area and circumference of a circle when its radius is given, provided by user. To do that we must make new function called 'calculatecircle' for example,

function [retval] = calculatecircle (x)

This code will also prompt a set of messages informing the user about license.


1.png

Later if you want you can delete the lines. After generating the function we should ask the user to enter a radius value that we will use to calculate the properties of circle.

r = input('Enter a radius value');

Then above line will generate a variable called r that can get any value according to the users answer to 'Enter a radius value' question. To improve it little bit and get a more user friendly result we may add one empty line after the question by changing the code into,

r = input('Enter a radius value:');

Then according to this value we must calculate the area and circumference . The area of a circle can be found by simply multiplying the radius with itself and pi (pirr) The circumference can be found by multiplying the radius with 2 and pi (2pir). So to illustrate this below code was written,

areaa = pi * r * r;
circumfrence = pi * 2 * r;


Then as a final step to display these values a printf function was called and the area and circumference values displayed,

printf(' area : %f meters, \n circumfrence : %f meters \n ',areaa,circumfrence);

Finally the function is ended closed with endfunction tag. The entire function became,

function [retval] = calculatecircle (x)
r = input("Enter a radius value:\n");
areaa = pi * r * r;
circumfrence = pi * 2 * r;
printf(' area : %f meters, \n circumfrence : %f meters \n ',areaa,circumfrence);
endfunction

1.png

So the output for different radius values is shown in the window panel. The results checked by our own calculator and they are quite accurate since Octave takes pi as 3.1416. In this exercise we wrote a simple function capable of calculating area and circumference of a circle. In our next tutorial, we will improve this function to give an error when negative radius value is written, calculate areas of different polygons and round the results into two decimal units to obtain a better result.


As a second beginning task we are wrote a function that sums three user entered numbers,

a=input(' Enter first digit ');
b=input(' Enter second digit');
c=input(' Enter third digit ');


Then we can take the arithmetical mean of these numbers and display it on print panel,



ort=(a+b+c)/ 3;
fprintf ('Mean of the entered numbers = %f', ort);


Finally we can run our function and test it,

1.png

Code to obtain arithmetical sum of three numbers,

function [retval] = suma (input1, input2)
a=input(' Enter first digit ');
b=input(' Enter second digit ');
c=input(' Enter third digit ');
ort=(a+b+c)/ 3;
fprintf ('Mean of the entered numbers = %f', ort);
endfunction

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @wodsuz I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.23
TRX 0.26
JST 0.040
BTC 97955.34
ETH 3472.91
USDT 1.00
SBD 3.29