0% found this document useful (0 votes)
166 views8 pages

Experiment1 Octave

This document serves as a lab manual for introducing students to GNU Octave and its application in symbolic computations relevant to calculus. It covers installation instructions, basic programming commands, arithmetic operations, control structures, and symbolic computations like differentiation, integration, and solving equations. Additionally, it includes exercises for practical application and sample solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views8 pages

Experiment1 Octave

This document serves as a lab manual for introducing students to GNU Octave and its application in symbolic computations relevant to calculus. It covers installation instructions, basic programming commands, arithmetic operations, control structures, and symbolic computations like differentiation, integration, and solving equations. Additionally, it includes exercises for practical application and sample solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment 1: Introduction to Calculus through

OCTAVE - Symbolic Computations

Objective

To introduce students to GNU Octave basics and apply symbolic computations for limits, derivatives,
integrals, and solving equations—tools essential for calculus and differential equations.

Part A: Introduction to GNU Octave Programming

Installation Instructions

1. Install GNU Octave: Download from https://www.gnu.org/software/octave/. Then click


on the ‘Download’ tab and choose suitable version based on your system’s configuration. The
interface is as follows:

Figure 1.1: Octave Interface

3
VIT, Vellore BAMAT101: Calculus Lab Manual

Figure 1.2: Octave Interface

2. Install Symbolic Package: For symbolic computations, you need to install the Symbolic package.
Type the following line in the Command Window (only once):
pkg install -forge symbolic

3. Load Package (do this at the start of each session):


pkg load symbolic

GNU Octave as a Computational Tool


GNU Octave is a high-level programming language primarily intended for numerical computations. It
is largely compatible with MATLAB and provides a powerful environment for mathematical modeling,
numerical computations, symbolic algebra, data analysis, and visualization. Octave code can be written
as scripts or functions, executed in the Command Window or saved as .m files.

Essential Commands

Command Description
clc Clears the command window
clear Clears all variables from memory
whos Displays workspace variables and memory info
help <command> Shows help for a specific command
doc <command> Opens documentation for a command
pkg list Lists installed packages
pkg load symbolic Loads the symbolic package

Table 1.1: Basic GNU Octave Environment Commands

Note: The contents of the workspace persist between the executions of separate commands. Therefore,
it is possible for the results of one problem to have an effect on the next one. To avoid this possibility,
it is a good idea to issue a clear command at the start of each new independent calculation.

Page 4
VIT, Vellore BAMAT101: Calculus Lab Manual

Arithmetic and Relational Operations


• Addition: +, Subtraction: -, Multiplication: *, Division: /, Power: ^

• Relational: ==, >, <, >=, <=, != (Note: Octave uses != instead of ~=)

Precedence Mathematical operations


First The contents of all parentheses are evaluated first, starting from
the innermost parentheses and working outward.
Second All exponentials are evaluated, working from left to right
Third All multiplications and divisions are evaluated, working from left
to right
Fourth All additions and subtractions are evaluated, starting from left to
right

Table 1.2: Hierarchy of arithmetic operations

Variables and Arrays

a = 5; % Scalar
v = [1 2 3]; % Row vector
v2 = [1; 2; 3]; % Column vector
m = [1 2; 3 4]; % 2x2 Matrix
z = zeros(3); % 3x3 Zero matrix
i = eye(4); % 4x4 Identity matrix

Accessing and Modifying Elements

A = [1 2 3; 4 5 6];
A(1,2) % Element at 1st row, 2nd column
A(:,1) % All rows, 1st column
A(2,:) = [7 8 9]; % Modify 2nd row

Controlling the Appearance of Floating Point Numbers


Octave by default displays numbers in a short format. The command format controls how the results
of computations are displayed. Fixed point format with 5 significant figures (default).

format short
x = -163.6667

Output: x = -163.67
If we want to see more digits, we use the command format long (fixed point format with 16 significant
figures):

format long
x = -163.66666666666666666666667

Output: x = -163.6666666666667
To return to the standard format, enter format short, or simply format.

Page 5
VIT, Vellore BAMAT101: Calculus Lab Manual

Elementary Functions

cos(x) Cosine abs(x) Absolute value


sin(x) Sine sign(x) Signum function
tan(x) Tangent max(x) Maximum value
acos(x) Arc cosine min(x) Minimum value
asin(x) Arc sine ceil(x) Round towards +∞
atan(x) Arc tangent floor(x) Round towards −∞
exp(x) Exponential round(x) Round to nearest integer
sqrt(x) Square root rem(x) Remainder after division
log(x) Natural logarithm angle(x) Phase angle
log10(x) Common logarithm conj(x) Complex conjugate

Table 1.3: Elementary functions

Predefined Constants

pi The π number, π = 3.14159


√ ...
i, j The imaginary unit i, −1
Inf The infinity, ∞
NaN Not a number

Table 1.4: Predefined constant values

Displaying Output: disp() and fprintf()


• disp(x): Displays the value of x without printing the variable name.
x = 5;
disp(x)

Output:
5
• fprintf() : Provides formatted output. Allows control over string and number display.
x = 5;
fprintf('The value of x is %d\n', x)

Output:
The value of x is 5

Control Structures
For Loop

for i = 1:5 % increment by 1 (default)


disp(i^2)
end

Output: 1 4 9 16 25
for i = 1:2:5 % increment by 2
disp(i^2)
end

Output: 1 9 25
Try the following code and observe the output:
for i = 5:-1:1 % decrement by 1
disp(i^2)
end

Page 6
VIT, Vellore BAMAT101: Calculus Lab Manual

While Loop

i = 1;
while i <= 5
disp(i^2)
i = i + 1;
end

Output: 1 4 9 16 25

If-Else

x = 10;
if x > 0
disp('Positive')
elseif x == 0
disp('Zero')
else
disp('Negative')
end

Output: Positive

Creating Functions
Open a new script to create your own function and save the file as the name of the function with .m
extension.
function y = squareNum(x)
y = x^2;
end

Save the file as squareNum.m

Part B: Symbolic Computations in GNU Octave


Important: Before starting symbolic computations, make sure to load the symbolic package:
pkg load symbolic

Declaring Symbolic Variables


syms x y
% Alternative method:
% x = sym('x');
% y = sym('y');

Symbolic Expressions
f = x^2 + 3*x + 2;
subs(f, x, 2) % Substitutes x = 2

Output: 12

Differentiation
Using Definition (First Principles)
The derivative is defined as:
f (x + h) − f (x)
f ′ (x) = lim
h→0 h

Page 7
VIT, Vellore BAMAT101: Calculus Lab Manual

pkg load symbolic


syms x h
f = x^2;
df = limit((subs(f, x, x+h) - f)/h, h, 0)

Output: df = 2*x

Using Inbuilt diff Function

pkg load symbolic


syms x
diff(x^3 + 2*x, x)

Output: 3*xˆ2 + 2

Integration

pkg load symbolic


syms x
int(x^2) % Indefinite
int(x^2, x, 0, 1) % Definite

Output:

x^3/3
1/3

Limits

pkg load symbolic


syms x
limit(tan(x)/x, x, 0)

Output: 1

Equation Solving

pkg load symbolic


syms x
solve(x^2 - 4 == 0, x)

Output:

ans =
-2
2

Partial Derivatives

pkg load symbolic


syms x y
f = x^2*y + y^2;
diff(f, x)
diff(f, y)

Output:

ans = 2*x*y
ans = x^2 + 2*y

Page 8
VIT, Vellore BAMAT101: Calculus Lab Manual

Key Differences Between MATLAB and Octave


1. Symbolic Package: In Octave, you must explicitly install and load the symbolic package

2. Not Equal Operator: Octave uses != instead of MATLAB’s ~


=

3. Package Management: Octave uses pkg commands for package management

4. Some Functions: Some advanced functions may have different names or may not be available

Exercises
1. Write an Octave script that computes squares of numbers from 1 to 10 using a loop.

2. Create a 3x3 identity matrix and add it to a 3x3 zero matrix.

sin(x)
3. Compute lim using symbolic limit.
x→0 x
4. Find the first derivative of f (x) = sin(x2 ).
Z 1
5. Compute the definite integral x3 dx.
0

∂ 2 ∂ 2
6. Find (x + xy) and (x + xy).
∂x ∂y

Sample Solutions
Exercise 1: Squares using loop

for i = 1:10
fprintf('Square of %d is %d\n', i, i^2)
end

Exercise 2: Matrix operations

I = eye(3); % 3x3 identity matrix


Z = zeros(3); % 3x3 zero matrix
result = I + Z; % Add them together
disp(result)

Exercise 3: Symbolic limit

pkg load symbolic


syms x
result = limit(sin(x)/x, x, 0)

Exercise 4: Derivative of composite function

pkg load symbolic


syms x
f = sin(x^2);
df = diff(f, x)

Page 9
VIT, Vellore BAMAT101: Calculus Lab Manual

Exercise 5: Definite integral


pkg load symbolic
syms x
result = int(x^3, x, 0, 1)

Exercise 6: Partial derivatives


pkg load symbolic
syms x y
f = x^2 + x*y;
df_dx = diff(f, x)
df_dy = diff(f, y)

Page 10

You might also like