add up a signal for every timestep

hello i've got an embedded matlab function in my model, which creates an output signal(wastage of fuel). Now i want to add up this signal for every timestep, so that i obtaine the total wastage after the simulation. Is there a way? Hope that was understandable ;)

2 Comments

You want to add the signal to what?
I mean i want to sum the wastage after every timestep so that i get the total wastage after the simulation.

Sign in to comment.

 Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 16 Mar 2013
Edited: Azzi Abdelmalek on 16 Mar 2013
Ok, you want a cumulative sum:
In your embedded function add these codes
at the beginning
function [utput,cum_output]=fcn(....)
persistent cum_output
if isempty(cum_output)
cum_output=0
end
and at the end of your code
cum_output=cum_output+output

11 Comments

okay, am i right? B is the function for wastage of fuel. A is another function.
function [B,cum_B,A]= engine(....)
persistant cum_B
if isempty(cum_B)
cum_B=0
end
"B=....."
cum_B=cum_B+B
hope i understood this right
okay, i tried and get an error: "The PERSISTENT declaration cannot be used on function outputs.
Function 'Subsystem1/Embedded MATLAB Function' (#69.60.70), line 2, column 1: "persistent" Launch diagnostic report."
here is the code:
function [A,B,cum_B]= wka(q,ow,uw,eta_get,eta_gen,n,dt,P_E)
persistent cum_B
if isempty(cum_B)
cum_B=0;
end
%#eml
A = (ow-uw)*q*eta_gen*eta_get;
Pi=P_E(end);
B=0.5*(Pi+P)*dt;
cum_B=cum_B+B;
have you an idee what is wrong?
It's right, try this
function [B,cum_B,A]= engine(....)
persistent cum_B1
if isempty(cum_B1)
cum_B1=0
end
"B=....."
cum_B1=cum_B1+B
cun_B=cum_B1
%And it's persistent (with e) not persistant
okay, it seems to work, at least there is no error. But strangely the difference in the scope block in comparison to before is extrem, and the outputsignal from A also changed...
function [A,B,cum_B]= wka(q,ow,uw,eta_get,eta_gen,n,dt,P_E)
persistent cum_B1
if isempty(cum_B1)
cum_B1=0;
end
%#eml
A = n*9.81*(ow-uw)*q*eta_gen*eta_get;
Pi=P_E(end);
B = 0.5*(Pi+P)*dt;
cum_B1=cum_B1+B;
cum_B=cum_B1;
i don't see the mistake
I noticed that you are not working with the same code, you've posted two different codes.
thats right, i tried a code from an other nearly identical model to see if it works with it.
its strange, it seems that the signal B hasn't changed. Signal B needs signal A to be calculates(in the last code i post, P and Pi have to be A and Ai!). Could this be a problem?
another idee: I put my signal in a "sum" block, split the output from the "sum" block and return one part of the output in a unit delay block and than reenter in the "sum" block.
Hope this is understandable.
st, I can't answer this question, because you did not provide enough information. What is sure is the code I added allows you to do cumulative sum.
ok, understand that, even so thank you for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink 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!