Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Reduction functions

Subject: Reduction functions

From: Kagome

Date: 27 Apr, 2011 21:37:04

Message: 1 of 2

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.

Subject: Reduction functions

From: Florin Neacsu

Date: 27 Apr, 2011 23:45:21

Message: 2 of 2

"Kagome" wrote in message <ipa2a0$gkp$1@fred.mathworks.com>...
> 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.

Hello,

I am using matlab 2007b and reduce is a function of robust control toolbox. I do not know if it is the case for you, but I would suggest changing the name.
Also, if I am not mistaking in your case reduce is a recursive function so I suggest using a stopping criteria (I did not see one; if it is there discard this).

Regards,
Florin

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us