# Need to calculate the power of battery

7 views (last 30 days)
Ajit Bashyal on 4 Mar 2021
Commented: Ajit Bashyal on 18 Mar 2021
Hello there,
Please kindly help me to write the matlab function code to calculate the power of the battery. Actually, the problem is that I have the I have a cost function whose output is battery power and there are several inputs. But the equation to calculate the input variables also depend on the output of previous time which is currently unknown. I can give you some example here:
function [X(t)]=batterypower(a(t)+b(t)+c(t)+d(t))
a(t)=b(t-1)+c(t-1)/X(t-1);
b(t)=a(t-1)/d(t-1);
c(t)=X(t-1)-d(t-1)/a(t-1);
d(t)=c(t-1)*b(t-1)/a(t-1)
where t=0:100 %time
I used for loop but still cannot get the result.
This is just the example but my problem is also similar. As we can see the input variable also depend on past output variable which is not yet calculated. How can write this in matlab function code. If anybody can help me, please I would be very thankful to him/her.

Prudhvi Peddagoni on 8 Mar 2021
Hi,
You can create arrays beforehand and try to fill it from t = 1 to 100.
a = zeros(1,100);
b = zeros(1,100);
c = zeros(1,100);
d = zeros(1,100);
X = zeros(1,100);
%Initialise
a(1) = 1;
b(1) = 1;
c(1) = 1;
d(1) = 1;
X(1) = 1;
for i = 1:100
[a,b,c,d,X] = batterypower(a,b,c,d,X,i);
end
function [a,b,c,d,X] = batterypower(a,b,c,d,X,t)
a(t)=b(t-1)+c(t-1)/X(t-1);
b(t)=a(t-1)/d(t-1);
c(t)=X(t-1)-d(t-1)/a(t-1);
d(t)=c(t-1)*b(t-1)/a(t-1)
Hope this helps.
Ajit Bashyal on 18 Mar 2021
Thank you for your informative response. This helps a lot to solve my problem.