Asked by Ademir Silva
on 17 May 2018 at 17:50

Hi, I am new user of MATLAB. I need to create a decrement function. Example:

EcAcum = Ec- (Ec- (EcAcum-Input);

At where,

EcAcum: Available kinetic energy; Ec: Total kinetic energy; Input: Field instantaneous energy;

My goal is to start the decrease from the EC value and decrease to zero.

I tried using Matlab Function but it did not work.

Opportunities for recent engineering grads.

Apply Today
## 8 Comments

## dpb (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569053

Well, let's see your attempt and try to work through how to get where you want to go from there as pedagogical exercise...

## Ademir Silva (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569065

When I use the Matlab function there is an error related to the EcAcum variable that was not denoted. In the expression

EcAcum = Ec- (Ec- (EcAcum-Input);

note that the variable appears twice. The second accuses the error. I did not know how to work around this problem.

## dpb (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569114

Well, you've not shown the code other than the one line in isolation so it's hard to discern what you think you're trying to do here...but note that algebraically

So that

probablyis not what you intended.If you're trying to iterate and subtract a portion, then

perhapsor somesuch similar expression...again w/o the full problem definition or code it's hard to know just precisely what the correct answer formulation would be.

## Ademir Silva (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569137

Hello, thank you for you atention.

This is the complete code. Very basic.

function EcAcum = fcn(in1, in2)

Ec = in1; Input = in2;

EcAcum = Ec-(Ec-(

EcAcum-Input));In this way the decree starts at Ec = in1 and decrements to the step of Input = in2.

I can not denote an initial value for the EcAcum variable. If I set, this variable will always be updated with the value set before and not calculated.

In this expression the error occurs in (EcAcum-Input); The fault that occurs is:

=> This variable 'EcAcum' might be used before it is defined.

Regards,

## Greg (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569143

As

dpbshowed you,is mathematically identical to

Which is meaningless if

EcAcumdoes not exist, which is exactly what MATLAB is telling you with the Code Analyzer messageThe variable 'EcAcum' might be used before it is defined.## Ademir Silva (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569158

Independent of the mathematical problem. how can I work around this problem without having to set EcAcum before? Even though I use the expression you have done,

deltaEc = Ec- (EcAcum-Input); EcAcum = Ec-deltaEc;

the error is the same. The algorithm requires that the variable be defined first. However, if I set it will not work.

## Greg (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569160

I don't understand the question. How can you possibly subtract a value from a variable that doesn't exist? You can't have a delta without an initial condition.

I think you want a loop, not a function. Re-reading that you're dealing with energy, I am

guessingyou are starting with some energy, and using it in bursts until you run out.## Ademir Silva (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401282-decrement-of-the-value-of-varivel-with-matlab-function#comment_569376

Your email:

"I do not understand the question. How can you possibly subtract a value from a variable that does not exist? You can not have a delta without an initial condition." That is exactly my difficulty. Different conditions:

1. Without the loop I need to set the variable to an initial condition. In this way the decrement calculation will not work. Why the defined variable restarts with the initial value during runtime;

2. With the loop I need to set the variable to an initial condition. In this way the decrement calculation works, but the output variable is updated only when it exits the loop. I need the updated value at runtime.

The ultimate goal of all this.

With a kinetic energy (EcAcum) available (flywheel for example) I will define in pu the mechanical power input of a synchronous machine to feed a load (kW). With the reduction of EcAcum the power of the synchronous machine will also decrease until it is exhausted.

Regards

Sign in to comment.