Plotting with a for loop

조회 수: 77 (최근 30일)
Jayme
Jayme 2014년 10월 1일
편집: Kay 2023년 8월 13일
I have a for loop and need to plot my final results. I have the hold on command in my code, but I still get only one point on my plot. What am I doing wrong?

채택된 답변

Star Strider
Star Strider 2014년 10월 1일
You probably have the plot command inside your loop.
Guessing as to your code, but it is best to do something like this instead :
for k = 1:n
x(k) = k;
y(k) = sin(x(k));
end
figure(1)
plot(x, y)
  댓글 수: 8
Jayme
Jayme 2014년 10월 2일
When I use this method, I have a completely blank graph. Where as before, I could get a point.
Star Strider
Star Strider 2014년 10월 2일
SUCCESS!
You don’t need the loop:
T_i=25;
T_infinity=800;
h=20;
t=325;
rho=720;
k=.16;
c=1255;
alpha=k/(rho*c);
u=((h*sqrt(alpha*t))/k);
x=0:.001:.1;
v=((h*x)/k);
w=(x/(2*sqrt(alpha*t)));
v=((h*x)/k);
w=(x/(2*sqrt(alpha*t)));
T=(((erfc(w)-exp(v+u^2).*erfc(w+u))*(T_infinity-T_i))+T_i);
plot(x,T);shg
produces:

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Esther Maria Ribezzo
Esther Maria Ribezzo 2020년 5월 13일
I have the same problem!!
for i=1:length(asse_x);
MLSE=(norm(AI_concatenated-(S*(A_TOT(:,i))))).^2;
plot(asse_x(i),MLSE, '*')
hold on
end

Moh'd Allouzi
Moh'd Allouzi 2021년 6월 13일
for k = 1:n
x(k) = k;
y(k) = sin(x(k));
end
figure(1)
plot(x, y)

alaa sleem
alaa sleem 2022년 1월 4일
cp=1.2
ta=30
hhv=50000
tex=130
k=1
for lamda =(.8,.1,1.5)
A_F(k)=lamda*x*(32+3.76*28)/(n*12+m*1)
if lamda < 1
N_CO2= x*((2*lamda)-1)-(m/4);
n_CO= x*((2*lamda)-1)-(m/4);
n_h2o=m/2;
n_n2=(lamda*x*3.76);
Total=N_CO2+n_CO+n_n2+n_h2o;
Xi_co2(k)=N_CO2/Total;
Xi__CO(k)=n_CO/Total;
Xi_h2o(k)=n_h2o/Total;
Xi_N2(k)=n_n2/Total;
#______________________________________________lean_____________________________________________
else lamda >= 1
N_CO2= n;
n_O2= x*(lamda-1);
n_h2o=m/2;
n_n2=(lamda*x*3.76);
Total=N_CO2+n_O2+n_n2+n_h2o;
Xi_co2(k)=N_CO2/Total;
Xi_o2(k)=n_O2/Total;
Xi_h2o(k)=n_h2o/Total;
Xi_N2(k)=n_n2/Total;
end
t_f(k) = ta+(hhv/((1+lamda)*cp))
eta(k)= ((1+lamda)*cp*(t_f-tex))/hhv
lamd_list(k)=lamda
k=k+1
end
fig1 = figure(1);
ax1 = axes('Parent', fig1);
A_F_plot = plot(lamd_list, total_util);
%concentration
plot(lamd_list,Xi_co2)
hold.on
plot(lamd_list(:lenght(Xi_o2)),Xi_o2)
hold.on
plot(lamd_list(:lenght(Xi__CO)),Xi__CO)
hold.on
plot(lamd_list,Xi_h2o)
hold.on
plot(lamd_list,Xi_N2)
hold.off
%air fuel
plot(lamd_list,A_F)
%temp
plot(lamd_list,A_F)

Kay
Kay 2023년 8월 13일
편집: Kay 2023년 8월 13일
I can't seem to get this plot to work. It only pot a single point. How can I make it plot multiple points? I probably need a for loop but I don't know how to use it here. Thanks!
% Calculate PAE for a Time-Varying Complex Baseband Signal
Pdc = 10; % DC input power in watts
Gain_dB = 15; % Power amplifier gain in dB
% Convert gain from dB to linear scale
Gain = 10^(Gain_dB/10);
% Define the time vector and the baseband complex signal parameters
t = linspace(0, 1, 1000); % Time vector from 0 to 1 second
wc = 2*pi*10; % Carrier frequency in radians per second
% Define the time-varying envelope function (you can change this function as needed)
a_t = 1 + 0.5*sin(2*pi*5*t); % Amplitude envelope with variations over time
% Define the phase modulation function (you can change this function as needed)
phi_t = pi/4 * sin(2*pi*t); % Phase modulation with variations over time
% Generate the baseband complex signal x(t) = a(t) * exp(j*(wc*t + phi(t)))
x_t = a_t .* exp(1j*(wc*t + phi_t));
% Calculate the power of the baseband signal
P_baseband = sum(abs(x_t).^2) / length(x_t); % Average power of the signal
% Calculate the RF output power using the power amplifier gain
Pout = P_baseband * Gain;
% Calculate the Power Added Efficiency (PAE) using the formula: PAE = (Pout - Pdc) / Pdc * 100
PAE = (Pout - Pdc) / Pdc * 100;
% Display the PAE value
fprintf('Power Added Efficiency (PAE) = %.3f%%\n', PAE);
% Plot the PAE against the RF output power
figure(6)
plot(Pout, PAE, '-o','LineWidth', 2);
xlabel('RF Output Power (Pout) in Watts');
ylabel('Power Added Efficiency (PAE) in %');
title('PAE vs. Output Power for Time-Varying Complex Baseband Signal');
grid on;

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by