# How to plot a current based on active voltage?

14 views (last 30 days)
Anniken on 18 Oct 2023
Commented: Anniken on 18 Oct 2023
I am to plot a current line in a graphs based on a active voltage. When I plot it, 200+ graphs appear and i wonder if maybe it is a mistake to not include it as an array, but cant quite figure it out. Can anybode please help?:)
tspan = [0 0.5];
R = 8.314;
F = 9.648e4;
T = 80 + 273.15; %Conductivity reference temperature
ipp_0 = 0.67e-4;
A_m = 232;
b_ca = 0.55 ;
b_an = 1-b_ca ;
C_dl = 3.5e-2*A_m ;
u0 = 0;
ipp_1 =0;
[T,U] = ode15s(@(t,u) u_act(t,u,A_m,ipp_0,b_an,F,R,T,b_ca,C_dl),tspan,u0);
plot(T,U)
%im = A_m*ipp_0*(exp(b_an*F/R/T*U) - exp(-b_ca*F/R/T*U));
%ipp = (idl+im)/A_m
function i_ret = i(t)
if t< 0.2
i_ret = 100;
elseif t < 0.4
i_ret = 10;
else
i_ret = 100;
end
end
function dudt = u_act(t,u,A_m,ipp_0,b_an,F,R,T,b_ca,C_dl)
im = A_m*ipp_0*(exp(b_an*F/R/T*u) - exp(-b_ca*F/R/T*u));
idl = i(t) - im;
dudt = idl/C_dl;
ipp = (idl+im)/A_m;
figure
plot(T, ipp)
end
It is supposed to look something like this, but we are to find i_pp which is i/A_m.
Torsten on 18 Oct 2023
If differential equations have discontinuous parameters (like "i" in the above case), one should restart the optimizer with the solution obtained so far at the points of discontinuity instead of integrating over. That's why I arranged the code the way I did.

Torsten on 18 Oct 2023
Edited: Torsten on 18 Oct 2023
i = [100,10,100];
t = [0,0.2,0.4,0.6];
R = 8.314;
F = 9.648e4;
Temp = 80 + 273.15; %Conductivity reference temperature
ipp_0 = 0.67e-4;
A_m = 232;
b_ca = 0.55 ;
b_an = 1-b_ca ;
C_dl = 3.5e-2*A_m ;
T = [];
U = [];
u0 = 0;
for j = 1:numel(i)
fun= @(t,u) (i(j)-A_m * ipp_0 * (exp(b_an*F/(R*Temp)*u)-exp(-b_ca*F/(R*Temp)*u)))/C_dl;
tspan = [t(j) t(j+1)];
[Tstep,Ustep] = ode15s(fun,tspan,u0);
T = [T;Tstep];
U = [U;Ustep];
u0 = Ustep(end,:);
end
figure(1)
plot(T,U)
grid on
ifun = @(time) 0;
for j = 1:numel(t)-1
ifun = @(time)ifun(time)+i(j)*(time>=t(j))*(time<t(j+1));
end
for j = 1:numel(T)
iplot(j) = ifun(T(j));
im(j) = A_m*ipp_0*(exp(b_an*F/R/Temp*U(j)) - exp(-b_ca*F/R/Temp*U(j)));
idl(j) = iplot(j) - im(j);
end
figure(2)
plot(T,[iplot;idl;im].')
grid on
Anniken on 18 Oct 2023
This is really clever and i feel like its above my level of expertise in matlab and will try to understand the complex code you have provided! I am truly grateful for your help:))

Florian Bidaud on 18 Oct 2023
Well you have a plot inside your u_act function, which is gonna be called at each calculation step in ode15s.
If you remove them you get the plot you want
tspan = [0 0.5];
R = 8.314;
F = 9.648e4;
T = 80 + 273.15; %Conductivity reference temperature
ipp_0 = 0.67e-4;
A_m = 232;
b_ca = 0.55 ;
b_an = 1-b_ca ;
C_dl = 3.5e-2*A_m ;
u0 = 0;
ipp_1 =0;
[T,U] = ode15s(@(t,u) u_act(t,u,A_m,ipp_0,b_an,F,R,T,b_ca,C_dl),tspan,u0);
plot(T,U)
%im = A_m*ipp_0*(exp(b_an*F/R/T*U) - exp(-b_ca*F/R/T*U));
%ipp = (idl+im)/A_m
function i_ret = i(t)
if t< 0.2
i_ret = 100;
elseif t < 0.4
i_ret = 10;
else
i_ret = 100;
end
end
function dudt = u_act(t,u,A_m,ipp_0,b_an,F,R,T,b_ca,C_dl)
im = A_m*ipp_0*(exp(b_an*F/R/T*u) - exp(-b_ca*F/R/T*u));
idl = i(t) - im;
dudt = idl/C_dl;
ipp = (idl+im)/A_m;
end
##### 3 CommentsShow 1 older commentHide 1 older comment
Florian Bidaud on 18 Oct 2023
Edited: Florian Bidaud on 18 Oct 2023
The plots you have inside your function are not of any use for the solving
Anniken on 18 Oct 2023
Yeah i should remove it. But do you know how i can plot the ipp based on the u_act and time?