<< Chapter < Page Chapter >> Page >

Now, let's incorporate the input command in AcetyleneBottleInteractive.m as shown below and the subsequent figure:

% This script computes the volume of an acetylene bottle % user is prompted to enter% a radius r for a hemispherical top % a height h for a cylindrical partr=input('Enter the radius of acetylene bottle in meters '); h=input('Enter the height of cylindrical part of acetylene bottle in meters ');Vol_top=(2*pi*r^3)/3; % Calculating the volume of hemispherical top [m3] Vol_cyl=pi*r^2*h; % Calculating the volume of cylindrical bottom [m3]Vol_total=Vol_top+Vol_cyl % Calculating the total volume of acetylene bottle [m3]

AcetyleneBottle
Interactive script that computes the volume of acetylene cylinder.

The command window upon run will be as follows, note that user keys in the radius and height values and the same input values result in the same numerical answer as in example which proves that the computation is correct.

AcetyleneBottle
The same numerical result is obtained through interactive script.

The disp Function

As you might have noticed, the output of our script is not displayed in a well-formatted fashion. Using disp , we can control how text or arrays are displayed in the command window. For example, to display a text string on the screen, type in disp('Hello world!') . This command will return our friendly greeting as follows: Hello world!

disp(variable) can be used to display only the value of a variable. To demonstrate this, issue the following command in the command window:

b = [1 2 3 4 5]

We have created a row vector with 5 elements. The following is displayed in the command window:

>>b = [1 2 3 4 5] b =1 2 3 4 5

Now if we type in disp(b) and press enter, the variable name will not be displayed but its value will be printed on the screen:

>>disp(b) 1 2 3 4 5

The following example demonstrates the usage of disp function.

Now, let's open AcetyleneBottleInteractive.m file and modify it by using the disp command. First save the file as AcetyleneBottleInteractiveDisp.m , so that we don't accidentally introduce errors to a working file and also we can easily find this particular file that utilizes the disp command in the future. The new file should contain the code below:

% This script computes the volume of an acetylene bottle % user is prompted to enter% a radius r for a hemispherical top % a height h for a cylindrical partclc % Clear screen disp('This script computes the volume of an acetylene bottle')r=input('Enter the radius of acetylene bottle in meters '); h=input('Enter the height of cylindrical part of acetylene bottle in meters ');Vol_top=(2*pi*r^3)/3; % Calculating the volume of hemispherical top [m3] Vol_cyl=pi*r^2*h; % Calculating the volume of cylindrical bottom [m3]Vol_total=Vol_top+Vol_cyl; % Calculating the total volume of acetylene bottle [m3] disp(' ') % Display blank linedisp('The volume of the acetylene bottle is') % Display text disp(Vol_total) % Display variable

Your screen output should look similar to the one below:

This script computes the volume of an acetylene bottle Enter the radius of acetylene bottle in meters .3Enter the height of cylindrical part of acetylene bottle in meters 1.5The volume of the acetylene bottle is 0.4807

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, A brief introduction to engineering computation with matlab. OpenStax CNX. Nov 17, 2015 Download for free at http://legacy.cnx.org/content/col11371/1.11
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'A brief introduction to engineering computation with matlab' conversation and receive update notifications?

Ask