필터 지우기
필터 지우기

Help writing Reduce functions for arithmatic in matlab

조회 수: 2 (최근 30일)
Kagome
Kagome 2011년 4월 27일
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
Andrew Newell
Andrew Newell 2011년 4월 28일
What command did you use to call reduce?
Andrew Newell
Andrew Newell 2011년 4월 28일
What is the function toStr?

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2011년 4월 28일
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!

Translated by