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

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.

clc
clear all

INTRODUCTION

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

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/symbolicops_blog.m')
disp(' ')
disp('LAST REVISED')
disp('   January 17, 2012')
disp(' ')
ABSTRACT
   This program shows you to use symbolic variables in MATLAB
 
AUTHOR
   Sri Harsha Garapati, Daniel Miller and
Autar K Kaw of http://autarkaw.wordpress.com
 
MFILE SOURCE
   http://numericalmethods.eng.usf.edu/blog/symbolicops_blog.m
 
LAST REVISED
   January 17, 2012
 

INPUTS

syms t r s    % t r and s are now symbolic varibles
a  =t^2+r;
y  =t^3+a+s;

DISPLAYING INPUTS

disp('INPUTS')
disp('a=')
disp(a)
disp('y=')
disp(y)
INPUTS
a=
t^2 + r
 
y=
t^3 + t^2 + r + s
 

THE CODE

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

DISPLAYING OUTPUTS

disp('  ')
disp('OUTPUTS')
fprintf('  The symbolic equation is\n  %s\n\n',eqn)
  
OUTPUTS
  The symbolic equation is
  t^3 + t^2 + r + s