Skip to main content
Dev LibreTexts

2.9 Embedding Octave

  • Page ID
    2175
  • % Define a function handle
    f = @(x) sin(x) .* exp(-x/5);
    
    % Create a range of x values
    x = linspace(0, 10, 100);
    
    % Evaluate the function
    y = f(x);
    
    % Numerically approximate the derivative using diff
    dx = x(2) - x(1);
    dy = diff(y) / dx;
    x_mid = x(1:end-1) + dx/2;
    
    % Find a root near x = 3 using fzero
    root = fzero(f, 3);
    fprintf('A root of the function is approximately at x = %.4f\n', root);
    
    A root of the function is approximately at x = 3.1416
    

     

    • Was this article helpful?