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 日

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 月 1 日
編集済み: Jayme 2014 年 10 月 1 日
My plot is outside the the for loop.
for x=0:.001:.1
T=(((erfc(w)-exp(v+u^2)*erfc(w+u))*(T_infinity-T_i))+T_i)
end
plot(x,T);shg
Star Strider
Star Strider 2014 年 10 月 2 日
It seems ‘T’ is not a function of ‘x’, and ‘T’ is not being saved as a vector in the loop. Are ‘w’, ‘v’, or ‘u’ related to ‘x’ in any way?
Try this (or something like it with the appropriate value for ‘w’):
x=0:.001:.1;
for k = 1:length(x)
w = x(k);
T(k)=(((erfc(w)-exp(v+u^2)*erfc(w+u))*(T_infinity-T_i))+T_i);
end
plot(x,T);shg
Jayme
Jayme 2014 年 10 月 2 日
There are a few lines where I set up the dummy variables u and w, which are functions of x. They are located in the for loop, but I had deleted them accidently with my comments from my code when I pasted it into my previous comment.
for x=0:.001:.1
u=((h*sqrt(alpha*t))/k)
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)
end
plot(x,T);shg
Star Strider
Star Strider 2014 年 10 月 2 日
I’m lost. Are ‘h’, ‘u’, ‘v’, ‘t’ and ‘w’ vectors the same length as ‘x’? (BTW, where did my ‘k’ loop disappear to? In your loop here, ‘x’ isn’t doing anything except iterating through the same calculations 100 times without changing them.) You have to subscript ‘T’ as well in the loop in order to have it the same length as ‘x’.
Jayme
Jayme 2014 年 10 月 2 日
The variables h, k, alpha, and t are all constant. Thus u is constant. The variables v and w are just dummy variables that are defendant on x.
I am trying to solve a heat transfer problem. I have a given set of temperatures (T_i and T_infinity), a convection rate (h), and a material (which dictates k and alpha). I know the amount of time the heat is exchanged (t) and I need to plot the temperature (T) across the material--at different distances (x).
Here is my entire code:
% Given
T_i=25;
T_infinity=800;
h=20;
t=325;
%Find T
%Table A.3 for Oak Wood
rho=720;
k=.16;
c=1255;
%calculations
alpha=k/(rho*c);
hold on;
for x=0:.001:.1
%dummy varriables
u=((h*sqrt(alpha*t))/k)
v=((h*x)/k)
w=(x/(2*sqrt(alpha*t)))
%Solve for T
T=(((erfc(w)-exp(v+u^2)*erfc(w+u))*(T_infinity-T_i))+T_i)
end
plot(x,T);shg
Image Analyst
Image Analyst 2014 年 10 月 2 日
編集済み: Image Analyst 2014 年 10 月 2 日
Have an index counter instead of x like you have.
x = 0 : 0.001 : 1
for k = 1 : length(x)
u=((h*sqrt(alpha*t))/k);
v=((h*x(k))/k);
w=(x/(2*sqrt(alpha*t)));
T(k)=(((erfc(w)-exp(v+u^2)*erfc(w+u))*(T_infinity-T_i))+T_i);
end
plot(x, T, 'b-', 'LineWidth', 3);
grid on;
By the way, check to see if the k in the denominator inside the loop is supposed to be the loop index. I don't know where that k came from.
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 日

0 投票

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 日

0 投票

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 日

0 投票

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 日

0 投票

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;

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

製品

タグ

質問済み:

2014 年 10 月 1 日

編集済み:

Kay
2023 年 8 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by