Function generation in Octave from scratch part 2

in #utopian-io7 years ago (edited)

What Will I Learn?

Greetings, in this tutorial we will continue focusing on making, editting functions in Octave by doing elementary level tasks.

  • You will learn the usage and importance of 'if' cases,
  • You will learn Octave's panels and it's properties,
  • You will learn function writing in Octave,
  • You will learn transfering your problems into functions,
  • 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

To summarize, the second part of function generation in Octave tutorial will be mostly related with using if statement to solve and simply our problems. In order to that we must first remeber what we have done in part 1 and then manage a way to implement if case to the problems we solved in previous part.


In our part 1 we formed a function that is capable of calculating the area and circumference of a circle. To do that we asked the user to enter a radius value and simply put that value in area, circumfrence problems. So for instance when r = 2 the area and circumfrence should be approximately 12.56m.

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

Running the above code will give us an expected answer as shown below,
1.png


The problem with this code is it also gives same area and a minus circumference value when a negative number is entered. However in reality a radius cannot get a negative value since it represents the distance from the center of a circle its surface or border.


Thereby as a first task and usage of if statement we will implement an if part in the code capable of checking the entered number. If the entered number is negative then the function should prompt a warning message like "Radius cannot be negative!" To do that below code was written,

if (r<0) printf("Radius cannot be negative! \n");

The above text will be displayed whenever the user enters a negative number. Now we should do calculation when the user enters a number equal or greater than zero,

function [retval] = calculatecircle (x) r = input("Enter a radius value:\n"); if (r<0) printf("Radius cannot be negative! \n"); else areaa = pi * r * r; circumfrence = pi * 2 * r; printf(' area : %f meters, \n circumfrence : %f meters \n ',areaa,circumfrence); endif endfunction

Running the above code will generate an output looking like this,

1.png

So we can see that the above code calculates the area and circumfrence of a circle for all real numbers. In our following tutorials we will extend this number set into complex numbers and the calculation will be avaliable for all symetrical geometrical shapes.

Our second task is to write a function capable of calculating f(x) and g(x) as defined below,
1.png

Similar to the previous task, an if statement is required to check whether x is greater than zero or not. To fullfill that aim below code was written,

if ( x <=0 ) f=1/(1+x^2); g=1/(1+x^2+x); else f=1/(1+log(x)); g=1/(x+log(x)); end

This above code is actually the algorithmic representation of the provided mathematical formulas, Like when the user enters a x value smaller or equal to zero the f(x) function becomes one over one plus the square of x. Similar process for g and when x greater than zero this time we are using log function to take the common logaritmic value of the entered value (base 10).

function [retval] = dee (input1, input2) x=input("Enter a x value \n"); if ( x <=0 ) f=1/(1+x^2); g=1/(1+x^2+x); fprintf(' f : %g, \n g : %g \n ',f,g); else f=1/(1+log(x)); g=1/(x+log(x)); fprintf(' f : %g, \n g : %g \n ',f,g); endif endfunction

Running the above code will return the below output which is calculating the return value for our function upto 0.0001 percent accuracy. In our next tutorial we will continue extending functions and begin to apply for loops.

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