Skip to main content
Engineering LibreTexts

5.7: Matlab Functions with No Inputs

  • Page ID
    84406
  • \( \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}}\)

    Tags recommended by the template: article:topic

    By Carey A. Smith

    Functions can have outputs, even though they have no input arguments.

    These are some built-in Matlab functions with no inputs:

    date % Try it

    datetime % Try it

    The function definition of such a file would be like this:

    function [date1] = date()

    An interesting Matlab function with no inputs is:

    pi

    This is used as a constant, but it is an actual function. You could define your own functions for constants such as Avogadro’s number, the universal gravitational constant, the speed of light, or a parsec’s distance.

     

    Example \(\PageIndex{1}\) draw_ellipse.m

    This example is a function (draw to draw a circle or an ellipse. 

    It has 3 input variables, but no output variable.

    A figure should be opened before calling this function.

    function [ ] = draw_ellipse(rx, ry, color)
    % This function draws an ellipse or a circle
    %  by drawing short lines between 32 point on a circle
    % The inputs are:
    %  rx = width (x direction)
    %  height (y direction)
    %  color: One these: 'b', 'c', 'g', 'k', 'r', or 'y'
    % There is no returned value. 
    dth = pi/16;  % (radians) The angle between points
    theta = 0: dth : 2*pi;
    x1 = rx*cos(theta);
    y1 = ry*sin(theta);
    plot(x1,y1,color,'LineWidth',4)
    end

    After calling the function, the user should useaxis equal so that the plot has the correct shape.

    This script (draw_ellipse_example.m) demonstrates the draw_ellipse function

    figure;
    rx = 3
    ry = 2;
    draw_ellipse(rx, ry, 'b')
    axis equal
    grid on;
    title('draw\_ellipse Example')

    Solution

    ellipse_fig.jpg

    .

    This page titled 5.7: Matlab Functions with No Inputs is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.