Difference between revisions of "Octave"
| Line 184: | Line 184: | ||
For support, contact KENET at support@kenet.or.ke, consult the documentation at https://training.kenet.or.ke, or access the Open OnDemand portal at https://ondemand.vlab.ac.ke. | For support, contact KENET at support@kenet.or.ke, consult the documentation at https://training.kenet.or.ke, or access the Open OnDemand portal at https://ondemand.vlab.ac.ke. | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
---- | ---- | ||
'''Back to:''' [[KENET Open OnDemand | Easy HPC access with KENET Open OnDemand]] | '''Back to:''' [[KENET Open OnDemand | Easy HPC access with KENET Open OnDemand]] | ||
Latest revision as of 19:09, 10 January 2026
Contents
Octave (Remote Desktop) Tutorial - KENET HPC Cluster
Overview
GNU Octave is a high-level programming language primarily intended for numerical computations. It provides a convenient command-line interface for solving linear and nonlinear problems numerically and for performing other numerical experiments. Octave is largely compatible with MATLAB, making it an excellent free alternative for scientific computing and engineering applications.
Use Cases: Octave is particularly well-suited for numerical analysis and linear algebra computations, signal and image processing workflows, solving differential equations and numerical integration, engineering simulations and modeling, algorithm prototyping and testing, MATLAB code compatibility verification, and educational purposes for teaching numerical methods and scientific programming.
Access: Octave is available through Remote Desktop sessions on the KENET Open OnDemand web portal at https://ondemand.vlab.ac.ke
Code Examples: All code examples for this tutorial are available in our GitHub repository at https://github.com/Materials-Modelling-Group/training-examples
Prerequisites
Before using Octave, you should have an active KENET HPC cluster account with access to the Open OnDemand portal. Basic understanding of programming concepts and mathematical operations will be helpful. Familiarity with MATLAB syntax is advantageous but not required. Knowledge of linear algebra and calculus concepts will enhance your ability to utilize Octave's capabilities effectively.
Launching Octave
Step 1: Start Remote Desktop Session
Begin by logging into Open OnDemand at https://ondemand.vlab.ac.ke. Click the Interactive Apps menu and select Remote Desktop to launch a full desktop environment on a compute node.
Step 2: Configure Desktop Session
Configure the resources needed for your desktop session based on your computational requirements.
| Parameter | Description | Recommended Value |
|---|---|---|
| Partition | Queue for job execution | normal for standard computations
|
| Walltime | Maximum runtime in hours | 4-8 hours for interactive work
|
| CPU Cores | Number of processor cores | 4-8 cores for parallel operations
|
| Memory | RAM allocation | 16-32 GB depending on problem size
|
Step 3: Launch Octave from Desktop
Once your remote desktop session starts, you can launch Octave in several ways. Click Applications in the top menu bar, navigate to Development or Education, and select GNU Octave. Alternatively, open a terminal and type octave --gui to launch the graphical interface, or simply type octave for the command-line interface.
Quick Start Guide
Understanding the Octave Interface
The Octave GUI consists of several key components. The Command Window is where you type commands and see immediate results. The Workspace panel displays all variables currently in memory with their values and types. The Command History shows previously executed commands. The File Browser allows navigation of your file system. The Editor provides a text editor for creating and modifying script files.
Your First Octave Commands
Start with simple mathematical operations to familiarize yourself with Octave's syntax. The octave/examples/01_basic_operations.m file in our GitHub repository demonstrates fundamental operations including arithmetic, matrix creation, and basic functions. Type commands directly in the Command Window and press Enter to execute them.
Creating and Running Scripts
For reproducible analyses, save your commands in script files with a .m extension. Click File → New → Script in the Octave GUI to create a new script. Type your commands in the editor, save the file, and run it by clicking the Run button or typing the script name (without .m extension) in the Command Window.
Working with Matrices
Octave excels at matrix operations. Matrices are the fundamental data type in Octave. The octave/examples/02_matrix_operations.m file demonstrates creating matrices, performing matrix arithmetic, accessing elements, and using built-in matrix functions. Understanding matrix operations is crucial for effective use of Octave.
Common Tasks
Task 1: Plotting and Visualization
Octave provides comprehensive plotting capabilities through its graphics functions. The octave/examples/03_plotting.m file demonstrates creating line plots, scatter plots, 3D surface plots, and customizing plot appearance with titles, labels, and legends. Plots appear in separate figure windows and can be exported to various image formats including PNG, PDF, and SVG.
Multiple plots can be displayed in a single figure using the subplot function. Plot properties can be customized extensively including colors, line styles, marker types, and font sizes. The hold command allows adding multiple plot elements to the same axes.
Task 2: Solving Linear Systems
One of Octave's strengths is solving systems of linear equations. The octave/examples/04_linear_systems.m file shows how to solve Ax = b using the backslash operator, compute matrix inverses and determinants, perform LU and QR decompositions, and calculate eigenvalues and eigenvectors.
Octave uses efficient numerical algorithms from LAPACK and BLAS libraries, making it suitable for large-scale linear algebra problems. Always check the condition number of matrices before attempting to solve systems to avoid numerical instability.
Task 3: Signal Processing
Octave includes signal processing capabilities through the signal package. The octave/examples/05_signal_processing.m file demonstrates loading the signal package, generating and analyzing signals, applying filters, computing Fast Fourier Transforms, and designing digital filters.
Install the signal package if not already available using pkg install -forge signal. Load it before use with pkg load signal. The package provides functions similar to MATLAB's Signal Processing Toolbox.
Task 4: Solving Differential Equations
Octave provides ODE solvers for ordinary differential equations. The octave/examples/06_differential_equations.m file shows how to define ODE functions, use lsode for numerical integration, solve systems of ODEs, and visualize solutions. This is particularly useful for dynamic system modeling and simulation.
Define your differential equation as a function that returns derivatives, specify initial conditions and time span, then call lsode or other ODE solvers to obtain the numerical solution.
Task 5: Image Processing
The image package provides functions for loading, processing, and analyzing images. The octave/examples/07_image_processing.m file demonstrates reading images, converting between color spaces, applying filters and transformations, edge detection, and saving processed images.
Install the image package with pkg install -forge image and load it with pkg load image. The package includes functions similar to MATLAB's Image Processing Toolbox.
Tips & Best Practices
Performance Optimization
Vectorize operations whenever possible rather than using loops. Octave is optimized for vectorized code and matrix operations. Pre-allocate arrays before filling them in loops to avoid repeated memory allocation. Use built-in functions which are highly optimized rather than implementing algorithms from scratch. For very large problems, consider using sparse matrices when appropriate.
Monitor memory usage with the whos command which displays all variables and their sizes. Clear unnecessary variables with the clear command to free memory. Use the tic and toc functions to measure execution time and identify performance bottlenecks.
Code Organization
Organize related code into functions saved in separate .m files. Use meaningful variable names and add comments to explain complex operations. Structure long scripts into sections using cell mode with double percent signs. Keep the workspace organized by clearing unnecessary variables periodically.
Create a project directory structure with separate folders for scripts, functions, data, and results. Use relative paths when loading data to make code portable across different systems.
Debugging and Testing
Use the keyboard function to pause execution and examine variables interactively. Set breakpoints in scripts by clicking in the left margin of the editor. Step through code line by line using the debugger toolbar. Display intermediate results using disp or printf for troubleshooting.
Test functions with simple inputs before using them on complex problems. Verify results against known solutions when available. Check for NaN or Inf values that might indicate numerical issues.
Example Workflows
Example 1: Engineering Simulation
Objective: Simulate beam deflection under load and visualize results.
Follow the complete workflow in octave/workflows/01_beam_deflection.m which demonstrates defining beam parameters and loading conditions, calculating deflection using differential equations, plotting deflection curves, computing maximum deflection and stress, and exporting results to CSV and images.
This workflow shows how to combine Octave's mathematical capabilities with visualization to solve a practical engineering problem.
Example 2: Signal Analysis
Objective: Analyze audio or sensor signals using Fourier analysis and filtering.
The octave/workflows/02_signal_analysis.m workflow demonstrates loading signal data, computing power spectral density, identifying dominant frequencies, designing and applying filters, and comparing filtered and original signals.
This is useful for noise reduction, feature extraction, and understanding signal characteristics in various applications from audio processing to sensor data analysis.
Example 3: Numerical Methods
Objective: Solve a nonlinear equation using numerical methods and compare different approaches.
Follow octave/workflows/03_numerical_methods.m which shows implementing Newton-Raphson method, using fzero for root finding, comparing convergence rates, and visualizing solution paths.
Understanding different numerical methods and their characteristics is crucial for choosing the right approach for specific problems.
Troubleshooting
Problem: Octave GUI won't start
If the Octave GUI fails to launch from the applications menu, try opening a terminal and running octave --gui to see any error messages. Check that your desktop session has sufficient memory allocated. If the GUI is problematic, use the command-line version by typing octave in the terminal, which provides full functionality without the graphical interface.
Problem: Package installation fails
Package installation requires internet access and may fail due to network issues or missing dependencies. Try installing packages manually by downloading them from Octave Forge at https://octave.sourceforge.io/ and using pkg install package.tar.gz. Some packages require system libraries that may need to be installed by KENET support.
Problem: Plot windows not displaying
If plots do not appear, check the graphics toolkit with graphics_toolkit and try switching toolkits with graphics_toolkit('gnuplot') or graphics_toolkit('qt'). Ensure your remote desktop session has proper graphics support. If problems persist, save plots directly to files using print or saveas rather than displaying them interactively.
Problem: Out of memory errors
Large matrix operations can exhaust available memory. Use sparse matrices for problems with mostly zero elements. Process data in chunks rather than loading everything into memory. Clear unnecessary variables frequently with clear. Request more memory when launching your desktop session if working with very large datasets.
Problem: MATLAB compatibility issues
While Octave aims for MATLAB compatibility, some functions may behave differently or not exist. Check the Octave documentation for equivalent functions. Use the exist function to test if a function is available before calling it. Some MATLAB toolboxes have equivalent Octave packages that need to be installed separately.
Additional Resources
The official GNU Octave documentation is available at https://octave.org/doc/interpreter/ and provides comprehensive coverage of all functions and features. The Octave Wiki at https://wiki.octave.org/ contains tutorials, examples, and community contributions. Octave Forge at https://octave.sourceforge.io/ hosts additional packages extending Octave's capabilities.
For numerical methods, "Numerical Methods using MATLAB" textbooks often work well with Octave. The MATLAB documentation at https://www.mathworks.com/help/matlab/ can also be helpful as most concepts transfer directly to Octave.
Code Examples Repository: All code examples referenced in this tutorial are available at https://github.com/Materials-Modelling-Group/training-examples
For support, contact KENET at support@kenet.or.ke, consult the documentation at https://training.kenet.or.ke, or access the Open OnDemand portal at https://ondemand.vlab.ac.ke.