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

조회 수: 1 (최근 30일)
Hans Wagener
Hans Wagener 2022년 12월 12일
편집: Fangjun Jiang 2022년 12월 12일
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

채택된 답변

VBBV
VBBV 2022년 12월 12일
편집: VBBV 2022년 12월 12일
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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2022년 12월 12일
편집: Fangjun Jiang 2022년 12월 12일
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 2022년 12월 12일
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.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by