tags:

views:

58

answers:

1

Suppose we have expression like eq1=sin(t)*cos(t)^2-sin(t)*cos(t)^4.We want to simplify this expression by simple command of matlab.We get different forms in the matlab prompt. How can we use one of the desired form among them like "1/16*sin(3*t)+1/8*sin(t)-1/16*sin(5*t)" without cutting and pasting?

Thanks in advance

Anant

+1  A: 

The SIMPLE command doesn't seem to offer any way to return all the different forms of the symbolic expression as an output argument. It only returns the simplest one, and displays the rest in the Command Window.

If you want to avoid having to cut and paste from the Command Window, you can use this function I wrote, which makes use of the EVALC command (as suggested by Andrew Janke) to capture the output displayed in the Command Window:

function allEquations = simple_forms(S)

  output = evalc('simple(S);');             %# Capture the output from the
                                            %#   Command Window
  newlineIndex = find(output == char(10));  %# Get the indices of newlines
  lineSizes = diff([0 newlineIndex]);       %# Get the sizes of each line
  output = mat2cell(output,1,lineSizes);    %# Put the lines in a cell array
  output = deblank(output);                 %# Remove blank spaces
  emptyIndex = cellfun(@isempty,output);    %# Find the indices of empty lines
  output(emptyIndex) = [];                  %# Remove the empty lines
  allEquations = output(2:2:end);           %# Get the even lines (where the
                                            %#   formulae are)
  allEquations = cellfun(@sym,allEquations,...    %# Convert the formulae to
                         'UniformOutput',false);  %#   symbolic expressions

end

This function will return a cell array containing the symbolic forms of all the equations generated by SIMPLE. You just have to pick the one you want, like so:

>> eq1 = sym('sin(t)*cos(t)^2-sin(t)*cos(t)^4');  %# Create a symbolic equation
>> eqs = simple_forms(eq1);                       %# Get the different forms
>> eqs{1}                                         %# Pick the first formula

ans =

sin(3*t)/16 - sin(5*t)/16 + sin(t)/8
gnovice
Could you use evalc() to avoid messing with the tempfile? Might make it work inside code that's already running under diary(), too.
Andrew Janke
@Andrew: Good point. I overlooked EVALC. I'll post an updated solution shortly.
gnovice

related questions