How to plot a current based on active voltage?

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.

1 Comment

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.

Sign in to comment.

 Accepted Answer

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

1 Comment

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:))

Sign in to comment.

More Answers (1)

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 Comments

But we need to use the solution gived from the plot in dudt function? So we kind of need to collect the results from function dudt and implement them in maybe antoher function which plots the current ipp in regards to the time(x-axis) while take into consideration the function values.
The plots you have inside your function are not of any use for the solving
Yeah i should remove it. But do you know how i can plot the ipp based on the u_act and time?

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Asked:

on 18 Oct 2023

Commented:

on 18 Oct 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!