How can I create a solid line from the data within a for loop?

2 views (last 30 days)
Hi all,
I want to plot efficiency 'Eff' on the y-axis against current density 'i' on the x-axis. However, because the data is in the for loop I cannot produce a solid line. Can someone please explain how I can fix this?
clear all
close all
clc
%I-U curve
T = 80;
r1 = 4.45153e-5;
r2 = 6.88874e-9;
r = r1 + (r2*T);
s = 0.33824;
d1 = -3.12996e-6;
d2 = 4.47137e-7;
p = 30;
t1 = -0.01539;
t2 = 2.00181/T;
t3 = 15.24178/T^2;
t = t1 + t2 + t3;
U_rev = 1.229;
A = 0.25;
f11 = 478645.74;
f12 = -2953.15;
f21 = 1.03960;
f22 = -0.00104;
for i = 0:0.2:100
U = 1.48/(U_rev + ((r1+d1)+(r2*T)+(d2*p))*(i*50) + (s * log((t*(i*50))+1)));
F = (((i*50)^2)/(f11+(f12*T)+(i*50)^2)*(f21+(f22*T)));
Eff = F*U;
plot(i,Eff,'.'); hold on
end

Accepted Answer

VBBV
VBBV on 12 Dec 2022
Edited: VBBV on 12 Dec 2022
k =1;
for i = 0:0.2:100
U(k) = 1.48/(U_rev + ((r1+d1)+(r2*T)+(d2*p))*(i*50) + (s * log((t*(i*50))+1)));
F(k) = (((i*50)^2)/(f11+(f12*T)+(i*50)^2)*(f21+(f22*T)));
Eff(k) = F(k)*U(k);
k = k+1;
end
plot(0:0.2:100,Eff);
Use an index to store outputs into variables

More Answers (2)

Fangjun Jiang
Fangjun Jiang on 12 Dec 2022
Edited: Fangjun Jiang on 12 Dec 2022
Typical way is to store the data in an array and then plot it once.
T=0:0.2:100;
Eff=zeros(size(T));
for k=1:length(T)
i=T(k);
U = 1.48/(U_rev + ((r1+d1)+(r2*T)+(d2*p))*(i*50) + (s * log((t*(i*50))+1)));
F = (((i*50)^2)/(f11+(f12*T)+(i*50)^2)*(f21+(f22*T)));
Eff(k) = F*U;
end
plot(T,Eff)

Steven Lord
Steven Lord on 12 Dec 2022
Either use an animatedline object or create the line before the loop starts and add points to its XData and YData properties inside the loop. I'll demonstrate the former technique.
h = animatedline;
axis([0 360 -1 1])
for thepoint = 0:360
addpoints(h, thepoint, sind(thepoint))
pause(0.01)
end
I used pause here but you could also use drawnow.

Categories

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