Octave for Chapter 1

The templates in this section provide sample Octave code for vector operations. You can access our code through the link at the bottom of each template. Feel free to modify the code and experiment to learn more!

You can write your own code using Octave software or online Octave cells. To access Octave cells online, go to the Sage Math Cell Webpage, select OCTAVE as the language, enter your code, and press EVALUATE.

To ”save” or share your online code, click on the Share button, select Permalink, then copy the address directly from the browser window. You can store this link to access your work later or share this link with others. You will need to get a new Permalink every time you modify the code.

Octave Tutorial

Basic Operations

Sometimes it is useful to reference a single component of a vector.

Loops

In this section we will introduce one of the most fundamental concepts in programming, a loop. Loops are used to repeat a procedure multiple times. We will start with a simple for loop. Every time you perform the steps inside the loop, you come back to the beginning, and increase the index by 1 until you reach the desired number of iterations.

Octave Exercises

Use Octave to find the angle (in degrees and radians) between vectors
Write Octave code to show that the points , , are the vertices of a right triangle in .
Use vectors and from Template temp_oct_vec_cross to illustrate that the cross product of two vectors is orthogonal to both vectors. Pick another pair of vectors and and repeat the demonstration.
% Define vectors v and w
 
v=[3;1;1];  
w=[-2;1;0];  
 
% Find the cross product v x w  
cross_v_w=cross(v,w)  
 
% Check to see that the cross product is orthogonal to both v and w  
dot(cross_v_w,v)  
dot(cross_v_w,w)

Link to code.

Write an Octave routine that allows the user to input three points and returns a normal vector to the plane containing these points. Your code might start as follows:
        % Enter coordinates of point A
 
        A=[1; 4; 6];  
 
        % Enter coordinates of point B  
        B=[-2; 3; 7]  
 
        % Enter coordinates of point C  
        C=[1; -2; 4]  
 
        % Normal vector to the plane containing A, B, C is given by  
 
    

What type of user inputs (other than all zeros) would result in the computed normal vector of ? Explain this phenomenon geometrically.

Use your code to find an equation of a plane containing points , , .

In this problem you will use the following definition of the dot product.
Due to a false fire alarm, a programmer left line 10 of the following code unfinished. Finish the code so that it computes the dot product (dp) of two vectors. Note that this code uses Definition def:dotproduct directly, instead of relying on the built-in dot function.
        % Define vectors v and w
 
        v=[3; 6; -1; 7; 12];  
        w=[-2; 4; -3; 5; 11];  
 
        % Initialize the dot product (dp)  
        dp=0;  
 
        % Find the dot product v*w directly from the definition  
        for i=1:5  
            dp=dp+;  
        end  
 
        % Print your answer  
        dp  
 
        % Check your answer  
        correct_answer=dot(v,w)  
    

Link to code.

Why did we have to initialize dp? What would happen if we didn’t?

2024-09-28 14:01:35