Help writing Reduce functions for arithmatic in matlab
Show older comments
I need help figuring out how to write a couple reduce functions. One is somewhat of an umbrella function and the second is a function to simplify an addition equation. Sorry, I know this looks like a lot. For the Umbrella function so far I have:
function Result=reduce(Expr)
fprintf('Reducing %s\n', toString(Expr))
type = Expr{1};
switch type
case {'Add', 'Sub', 'Mul', 'Div'}
Operand1 = reduce(Expr{2});
Operand2 = reduce(Expr{3});
Result = reduceArith(type, Operand1, Operand2);
case {'Neg'}
case {'Pow'}
case {'Sqrt'}
end
and for the reduceAdd function i have:
function Result = reduceAdd(Operand1, Operand2)
Operand1 = reduce(Expr{2});
Operand2 = reduce(Expr{3});
if isNum(Operand1)&& isNum(Operand2)
Result = {'Num', Operand1{2} + Operand2{2}};
else
Result={'Add', Operand1,Operand2};
end
end
There is also an intermediate function:
function Result = reduceArith(type, Operand1, Operand2)
type = Expr{1};
switch type
case{'Add'}
Result=reduceAdd(Operand1, Operand2);
case{'Sub'}
Result=reduceSub(Operand1, Operand2);
case{'Mul'}
Result=reduceMul(Operand1, Operand2);
case{'Div'}
Result=reduceDiv(Operand1, Operand2);
end
end
I thought that these all were correct but when I test out the reduce function i get this error:
Error in ==> reduce at 2
fprintf('Reducing %s\n', toString(Expr))
??? Output argument "Result" (and maybe others) not assigned during call to "F:\Milanel(UCONN)\CSE 1010\MATLAB\05 Symbolic Math\reduce.m>reduce".
Error in ==> reduce at 6
Operand1 = reduce(Expr{2});
If anyone could help me figure out how to correct the error I would greatly appreciate it. I know this seems like a lot to look at.
2 Comments
Andrew Newell
on 28 Apr 2011
What command did you use to call reduce?
Andrew Newell
on 28 Apr 2011
What is the function toStr?
Answers (1)
Walter Roberson
on 28 Apr 2011
0 votes
You need a default case that sets Result to the input if it is irreducible.
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!