Skip to main content

Section 8 Code examples

Subsection 8.1 Lab 1 - Introduction

#Symbolic math in Octave/Matlab --
#September 4, 2019
#Numerical Analysis

#to use symbolic math, we first need to load the symbolic package
#using the command pkg load symbolic
#once the package is installed and loaded, we can use the symbolic capabilities
#installed in Octave

syms x; #defines x as a symbolic variable
fun = sin(x); #defines the variable fun as a symbolic object sin(x)

#one useful command that works on symbolic objects is diff
#which is Octave's built in command for differentiation.

#diff(fun) takes the symbolic derivative of the symbolic function
#sin(x).
diff(fun);

#this can be stored as another variable
dfun = diff(fun);

#note that this object cannot be evaluted. If we want to turn it
#into a function, we can use the command function_handle.

dfun = function_handle(dfun);

dfun(pi/3) #evaluates the derivative at pi/3

#dfun is now a function that can be evaluated on scalars or arrays.
#it can also be plotted. the simplest possible plotting command is
#ezplot, which takes care of a lot of the mechanics for you. ezplot
#doesn't care if an expression is symbolic or a function
ezplot(dfun)

#if you want to plot multiple graphs on the same figure, you can use the
# hold command to keep the figure in place before the next plot
ezplot(fun)
hold on;
ezplot(dfun)
hold off;

#another useful mathematical command is factorial
factorial(3)

#putting this together, we might plot sin x against a Taylor
#polynomial
fun = sin(x);
T = x - x.^3/factorial(3) + x.^5/factorial(5) - x.^7/factorial(7);
ezplot(fun)
hold on;
ezplot(T)
hold off;

#we will look at more powerful visualizations in the next set of
#notes. ezplot is fast, but very limited in what can be controlled.
#we want to work numerically in general.
Listing 8.1. A first set of Octave/Matlab commands and examples
#Loops and arrays
#Numerical Analysis
#9/5/2019
#Ryan Tully-Doyle

#This set of examples will focus on loops and arrays, which are structures
#that we will be using constantly.
#A for loop runs over an index that goes through a prescribed set of numbers.
#The formatting is slightly different than other languages, but powerful
#in a mathematics context.

#the following loop will run for values of i starting at 1 and ending at 10.
#Each iteration will perform the same command.

for i = 1:10
  disp(i) #display the current value of i
end

#unlike other langauges, non-integer indicies can be used in octave/matlab.

for i = 1:.1:10 #starts at 1, counts by .1 until reaching 10
  disp(i) #display the current value of i
end

#it is useful to be able to exit a loop on a condition. in octave, this
#command is called break

for i=1:10
  disp(i)
  if i == 5
    printf("You have to stop now!\n") #\n tells printf to break the line
    break
  end
end

#Next, we'll look at how octave deals with arrays, or lists of numbers.
#Functions in octave by default can act on arrays or scalars without using loops

X = 12:17;
disp(X)

#you might wish to know how long an array is

length(X)

#you might like to know the largest element in an array

max(X)

#If you want to refer to a specific element in an array, you can extract it
#by invoking its index.

X(3) #calls the third entry of X

#You can take slices of arrays by using index ranges

X(3:5) #calls the third through fifth entry of X

#As octave is natively an array based language, vector operations are natural.
#arrays of the appropriate sizes can be added, subtracted and multiplied by scalars.

X=1:10;
Y = 2:11;
X + Y
Y - Y
2*X

#since mathematical functions are natively array functions, you can apply a function
#to an array.

sin(X)

#Every time you use an operation that might be ambiguous (things involving
#multiplication and subtraction, add a . to the operation to specify that you
#want the operation to be applied to each entry.

X.^2
Listing 8.2. Loops and arrays