Skip to main content
Mathematics LibreTexts

4.6.1: MATLAB/Octave program

  • Page ID
    143457
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Here is a program that runs in MATLAB or OCTAVE that performs the iteration of a linear function — where we are given the coefficients of the function and the starting value. The program outputs the iteration sequence.

    Needs to be debugged the olympic rings on the blue bar select the Jupyter hub

    function x=iterlin(a, b, x0, N)
    %function x=iterlin(a, b, x0, N)
    %
    %Iterates f(x)=ax+b
    %
    %Inputs
    % a - slope of function
    % b -intercept of function
    % x0 - initial condition
    % N - number of iterations
    %Output
    % x - vector of iterates
    x=zeros(1, N); %ititialize x as a vector of zeros
    x(1)=x0; %set initial condition
    for i=1:(N-1)
    x(i+1)=a*x(i)+b;
    print a %iterate
    end
    x=iterlin(0.5, 2, 1, 10)
    
    parse error:
    
      syntax error
    
    >>> plot(x, ’k-o’) %plot
                ^
    
    parse error:
    
      syntax error
    
    >>> xlabel(’i’) %label axes
               ^
    
    parse error:
    
      syntax error
    
    >>> ylabel(’x_i’)
               ^
    
    

    This code is a function that will work in \(M A T L A B\) or its free open source alternative Octave. It should be saved as iterlin.m and placed in a directory accessible to MATLAB/Octave. To do this make the directory your current directory or add it to your path in MATLAB/Octave.

    We can get information about the function by entering the following at the MATLAB/Octaveprompt.

    > help iterlin

    To run the function with \(\left(a, b, x_0, n\right)=(0.5,2,5,10)\) and save the output to \(x\) type:

    \(>x=i t e r l i n(0.5,2,5,10)\)
    We can plot the result and label the axes using:

    \(>\) plot \((\mathrm{x}\), ' \(\mathrm{k}-\mathrm{o}\) ') \(\%\) plot
    \(>\) xlabel('i') \%label axes
    > ylabel('x_i')

    While we have not done it for this lab, by altering the program line that calculates \(x(i+1)=a * x(i)+b\), you could use a modified form of your program to compute iterates of any reasonable function. (With functions that do not involve parameters \(a\) and \(b\), you can simply ignore the inputs for \(a\), and \(b\) - giving them any values you wish - or you can modify the first line of the program.)

     

     

     


    4.6.1: MATLAB/Octave program is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?