Plot generating blank graph?
2 次查看(过去 30 天)
显示 更早的评论
I have a code that stores values, calculates new values, then runs those calcuated variables through a for loop with two separate equations (Im1 and Im6), each with an unknown variable "t". However, when I ask MATLAB to plot the results of the loops, all I receive is a blank graph, as seen in the attachment. It is supposed to appear similar to the graph in the second attachment, with two lines for each of the equations. I don't know what is going wrong. I've added my current script below. Thank you.
l = 0.0001;
lambda = 0.405;
syms t;
wj11 = 0;
wj12 = -79.077;
wj61 = 0;
wj62 = -0.014533;
wj63 = -0.0682;
wj64 = -0.1947;
wj65 = -1.023;
wj66 = -2.896;
wj67 = -79.4;
Bj11 = 0.00097;
Bj12 = 0.99903;
Bj61 = Bj11;
Bj62 = 0.000091;
Bj63 = 0.000664;
Bj64 = 0.000901;
Bj65 = 0.001282;
Bj66 = 0.001292;
Bj67 = 0.9948;
Beta1m1 = (Bj11*lambda)/((wj11+lambda)^2);
Beta2m1 = (Bj12*lambda)/((wj12+lambda)^2);
Beta1m6 = (Bj61*lambda)/((wj61+lambda)^2);
Beta2m6 = (Bj62*lambda)/((wj62+lambda)^2);
Beta3m6 = (Bj63*lambda)/((wj63+lambda)^2);
Beta4m6 = (Bj64*lambda)/((wj64+lambda)^2);
Beta5m6 = (Bj65*lambda)/((wj65+lambda)^2);
Beta6m6 = (Bj66*lambda)/((wj66+lambda)^2);
LB1 = Beta1m6;
LB2 = LB1+Beta2m6;
LB3 = LB2+Beta3m6;
LB4 = LB3+Beta4m6;
LB5 = LB4+Beta5m6;
LB6 = LB5+Beta6m6;
for t=0:0.1:1
Im1 = (exp(wj11*t)/(l+Beta1m1))+(exp(wj12*t)/(l+Beta2m1));
I1m6 = (exp(wj61*t)/LB1);
I2m6 = (exp(wj62*t)/LB2);
I3m6 = (exp(wj63*t)/LB3);
I4m6 = (exp(wj64*t)/LB4);
I5m6 = (exp(wj65*t)/LB5);
I6m6 = (exp(wj66*t)/LB6);
Im6 = I1m6+I2m6+I3m6+I4m6+I5m6+I6m6;
end
disp(Im1)
disp(Im6)
plot(Im1,'LineWidth',2);
hold on
plot(Im6,'Linewidth',2)
2 个评论
Chuguang Pan
2024-10-13
The values of Im1 and Im6 in your for loop are reassigned repeatly, so that you get the scalar in the last loop. The plot function can not plot a standalone scalar value.
回答(2 个)
Image Analyst
2024-10-13
You need to index Im1 and Im6. Otherwise you're just overwriting a single number in your loop and not creating a vector of several values that can be plotted as a function of t. See corrected code below. Adapt as needed.
% Demo by Image Analyst.
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
lineWidth = 2;
markerSize = 30;
l = 0.0001;
lambda = 0.405;
% syms tIndex;
wj11 = 0;
wj12 = -79.077;
wj61 = 0;
wj62 = -0.014533;
wj63 = -0.0682;
wj64 = -0.1947;
wj65 = -1.023;
wj66 = -2.896;
wj67 = -79.4;
Bj11 = 0.00097;
Bj12 = 0.99903;
Bj61 = Bj11;
Bj62 = 0.000091;
Bj63 = 0.000664;
Bj64 = 0.000901;
Bj65 = 0.001282;
Bj66 = 0.001292;
Bj67 = 0.9948;
Beta1m1 = (Bj11*lambda)/((wj11+lambda)^2);
Beta2m1 = (Bj12*lambda)/((wj12+lambda)^2);
Beta1m6 = (Bj61*lambda)/((wj61+lambda)^2);
Beta2m6 = (Bj62*lambda)/((wj62+lambda)^2);
Beta3m6 = (Bj63*lambda)/((wj63+lambda)^2);
Beta4m6 = (Bj64*lambda)/((wj64+lambda)^2);
Beta5m6 = (Bj65*lambda)/((wj65+lambda)^2);
Beta6m6 = (Bj66*lambda)/((wj66+lambda)^2);
LB1 = Beta1m6;
LB2 = LB1+Beta2m6;
LB3 = LB2+Beta3m6;
LB4 = LB3+Beta4m6;
LB5 = LB4+Beta5m6;
LB6 = LB5+Beta6m6;
t = 0:0.1:1
for tIndex = 1 : numel(t)
this_t = t(tIndex); % Get this particular value of t from the vector of t values.
% Assign this particular value of Im1. Need to give it the index so it
% goes into the proper location of the vector.
Im1(tIndex) = (exp(wj11*this_t)/(l+Beta1m1))+(exp(wj12*this_t)/(l+Beta2m1));
I1m6 = (exp(wj61*this_t)/LB1);
I2m6 = (exp(wj62*this_t)/LB2);
I3m6 = (exp(wj63*this_t)/LB3);
I4m6 = (exp(wj64*this_t)/LB4);
I5m6 = (exp(wj65*this_t)/LB5);
I6m6 = (exp(wj66*this_t)/LB6);
% Assign this particular value of Im6. Need to give it the index so it
% goes into the proper location of the vector.
Im6(tIndex) = I1m6+I2m6+I3m6+I4m6+I5m6+I6m6;
end
disp(Im1)
disp(Im6)
plot(t, Im1, 'r.-', 'LineWidth', lineWidth, 'MarkerSize', markerSize);
hold on
plot(t, Im6, 'b.-', 'LineWidth', lineWidth, 'MarkerSize', markerSize)
grid on;
xlabel('t', 'FontSize', fontSize);
ylabel('Value of Im1 or Im6', 'FontSize', fontSize);
legend('Im1', 'Im6');
1 个评论
Image Analyst
2024-10-13
Or, the more MATLABy way of doing it, you can do it vectorized like Star showed you. That's actually how most of us would do it. I just showed you the for loop way because that's how you started it.
Star Strider
2024-10-13
You do not need the loop.
Just usee vectorisation —
l = 0.0001;
lambda = 0.405;
syms t;
wj11 = 0;
wj12 = -79.077;
wj61 = 0;
wj62 = -0.014533;
wj63 = -0.0682;
wj64 = -0.1947;
wj65 = -1.023;
wj66 = -2.896;
wj67 = -79.4;
Bj11 = 0.00097;
Bj12 = 0.99903;
Bj61 = Bj11;
Bj62 = 0.000091;
Bj63 = 0.000664;
Bj64 = 0.000901;
Bj65 = 0.001282;
Bj66 = 0.001292;
Bj67 = 0.9948;
Beta1m1 = (Bj11*lambda)/((wj11+lambda)^2);
Beta2m1 = (Bj12*lambda)/((wj12+lambda)^2);
Beta1m6 = (Bj61*lambda)/((wj61+lambda)^2);
Beta2m6 = (Bj62*lambda)/((wj62+lambda)^2);
Beta3m6 = (Bj63*lambda)/((wj63+lambda)^2);
Beta4m6 = (Bj64*lambda)/((wj64+lambda)^2);
Beta5m6 = (Bj65*lambda)/((wj65+lambda)^2);
Beta6m6 = (Bj66*lambda)/((wj66+lambda)^2);
LB1 = Beta1m6;
LB2 = LB1+Beta2m6;
LB3 = LB2+Beta3m6;
LB4 = LB3+Beta4m6;
LB5 = LB4+Beta5m6;
LB6 = LB5+Beta6m6;
% for t=0:0.1:1
t=0:0.1:1;
Im1 = (exp(wj11*t)/(l+Beta1m1))+(exp(wj12*t)/(l+Beta2m1));
I1m6 = (exp(wj61*t)/LB1);
I2m6 = (exp(wj62*t)/LB2);
I3m6 = (exp(wj63*t)/LB3);
I4m6 = (exp(wj64*t)/LB4);
I5m6 = (exp(wj65*t)/LB5);
I6m6 = (exp(wj66*t)/LB6);
Im6 = I1m6+I2m6+I3m6+I4m6+I5m6+I6m6;
% end
disp(Im1)
disp(Im6)
plot(Im1,'LineWidth',2);
hold on
plot(Im6,'Linewidth',2)
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!