Clear Filters
Clear Filters

Help writing Reduce functions for arithmatic in matlab

2 views (last 30 days)
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.

Answers (1)

Walter Roberson
Walter Roberson on 28 Apr 2011
You need a default case that sets Result to the input if it is irreducible.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!