Function generation in Octave from scratch part 4

in #utopian-io7 years ago (edited)

What Will I Learn?

Greetings, in this tutorial we will continue writing 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 loops

Difficulty

  • Basic

Tutorial Contents


1.png

In the previous tutorials we learned the usage and application of if and for statements. In this tutorial by exceeding our previous knowledge, we will focus on nested loops which is basicaly a loop inside another loop. Nested loops are used mainly when the user requires to move or check the range of numbers. In other words, when there are two variables needed to be observed. One of the most important fact about nested loops is that their outer loop changes only after their inner loop is completely finished. Moreover, although almost any loops can be extended into nested one the most common and simple one is nested for loop. Thereby, in this tutorial we will focus on nested 'for' loops in Octave.

To begin with and to understand the concept we shall do a simple task. We are aimed to do a multiplication table showing the multiplication result of the horizantal and vertical elements. To write the function as usual we should first define it as follows,



function [retval] = nestedfor (input1, input2)

Then we need to arrange, draw a table to do that a simple print function will be enough. These two lines of code generate a table like figure as shown below,



fprintf(' %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d \n',1,2,3,4,5,6,7,8,9,10); fprintf('-------------------------------------------\n');

1.png

Then we need to draw the vertical side of the table to do that we can use a simple for function which gives us the same nunber when multiplied by one.



for i=1:10 fprintf('%2d |',i);

However when multiplied by other numbers we should multiply the vertical and horizontal element. To do that need another loop inside the existing for that will use the element in the first loop and multiply it with the second generated one in the second loop as follows,



for i=1:10 fprintf('%2d |',i); for j=1:10 fprintf('%3d ',i*j); endfor

This above code will first generate a row of numbers from 1 to 10. Then when i is equal to one j values will vary from one to ten and for each j value we will have i*j multiplication. This result will also vary from one to ten since i is equal to one. Then we can print it and it will eventualy calculate, print the very first line of our table (multiplication by one). Then i will increase by one becoming two. Same procedure repeats itself resulting the multiplaction vary from 2 to 20 with an multiplication rate of 2.


Then we can leave an empty space at the end of the table to provide confusion with other codes,

fprintf('\n');

and completed form of our function,



function [retval] = nestedfor (input1, input2)


fprintf(' %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d \n',1,2,3,4,5,6,7,8,9,10);


fprintf('-------------------------------------------\n');


for i=1:10


fprintf('%2d |',i);


for j=1:10


fprintf('%3d ',i*j);


endfor


fprintf('\n');


endfor


endfunction


Running the above code will give us the below result,

1.png

To extend our knowledge and apply another example we will this time solve the below calculation by using the function property of Octave.

1.png

To solve that we again defined our function,

function [retval] = nestedfor (input1, input2)

Then we should define the numerator and denumerator of the function.

clc; sum1=0;

This above code will generate a variable named sum1 which will later be used as the result of the expression. Now we need to define the expression. By using a for loop initially denominator is defined from two to fifteen with an increase rate of one.

for i=2:15 fact1=1;

Now we need to define nominator. To do that a nested for loop was written. In this loop j values are dependent on i value. When i is eqaul to two j becomes one and making the factorial result equal to 1.

for j=1:i fact1=fact1*j; end

Then we need to calculate the mathematical operations. Summation and dividison of i to j as shown in the expression.

res = (i-1)/fact1; sum1 =sum1+res;

Finally display the overall result,

fprintf('result : %g |', sum1);

The overall code becomes,

function [retval] = nestedfor (input1, input2)


clc;


sum1=0;


for i=2:15


fact1=1;


for j=1:i


fact1=fact1*j;


end


res = (i-1)/fact1;


sum1 =sum1+res;


end


fprintf('result : %g |', sum1);

Once we run the code we obtain the below result,

1.png


In our next tutorial we will focus on break and while cases, quite useful when the user wants the loop to continue until a certain condition.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors
















Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • NexMirror/Octave repository is not a working mirror of the original repository since they are not synced. Last commit in the mirror is months ago, while the last update from the original repository is just minutes ago.

  • We have a white-list for unofficial repositories and mirrors for open source projects which doesn't have its official repository on GitHub. Here is the related links:

  • Approval of your prior contributions on this project are moderation mistakes. Please do not try to contribute with this repository again.

Suggestions:

  • You should not contribute for GNU Octave until there is an official repository on GitHub or an unofficial one whitelisted.
  • Next time, please check this whitelist and syncronization between mirror and the original repository.

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

Coin Marketplace

STEEM 0.24
TRX 0.25
JST 0.040
BTC 94242.56
ETH 3408.52
USDT 1.00
SBD 3.35