HOW DO I DO THAT IN MATLAB SERIES?

In this series, I am answering questions that students have asked me about MATLAB.

Contents

TOPIC

How to use symbolic variables in MATLAB using inline function

SUMMARY

Language : Matlab 2008a; Authors : Sri Harsha Garapati, Daniel Miller, Autar Kaw; Mfile available at Last Revised : January 17, 2012; Abstract: This program shows how use symbolic variables using inline function.

clc
clear all

INTRODUCTION

disp('ABSTRACT')
disp('   This program shows you to use symbolic variables in MATLAB using inline')

disp(' ')
disp('AUTHOR')
disp('   Sri Harsha Garapati, Daniel Miller and')
disp('Autar K Kaw of http://autarkaw.wordpress.com')
disp(' ')
disp('MFILE SOURCE')
disp('   http://numericalmethods.eng.usf.edu/blog/symbolic_inline_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 17, 2012')
disp(' ')
ABSTRACT
   This program shows you to use symbolic variables in MATLAB using inline
 
AUTHOR
   Sri Harsha Garapati, Daniel Miller and
Autar K Kaw of http://autarkaw.wordpress.com
 
MFILE SOURCE
   http://numericalmethods.eng.usf.edu/blog/symbolic_inline_blog.m
 
LAST REVISED
   January 17, 2012
 

INPUTS

% Function Y
y   =inline('t^2+7*t+9');
% find the value of y(a) using the inline notation
% a=4
a=4;

DISPLAYING INPUTS

disp('INPUTS')
disp('function, Y=')
disp(y)
fprintf(' The input value of a is %g\n', a)
INPUTS
function, Y=

     Inline function:
     y(t) = t^2+7*t+9

 The input value of a is 4

THE CODE

% find the value of y(a) using the inline notation
ya=y(a);

yeqn=char(y);  %char will convert the equation to a string

DISPLAYING OUTPUTS

disp('  ')
disp('OUTPUTS')
fprintf('  The function is: y(t)=%s\n',yeqn)
fprintf('  y(%g)=%g\n\n',a,ya)
  
OUTPUTS
  The function is: y(t)=t^2+7*t+9
  y(4)=53