Plot generating blank graph?

2 views (last 30 days)
Zachary
Zachary on 13 Oct 2024
Commented: Image Analyst on 13 Oct 2024
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 Comments
Chuguang Pan
Chuguang Pan on 13 Oct 2024
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.
Zachary
Zachary on 13 Oct 2024
I'm sorry, I'm confused. Why can't MATLAB take simplified values (with one variable) and plot them between a range?

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 13 Oct 2024
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);
Beginning to run LiveEditorEvaluationHelperEeditorId.m ...
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
t = 1×11
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
Columns 1 through 6 6447.75687139828 403.016364430728 400.792505741751 400.791687584634 400.791687283634 400.791687283523 Columns 7 through 11 400.791687283523 400.791687283523 400.791687283523 400.791687283523 400.791687283523
disp(Im6)
Columns 1 through 6 1208.32551171607 1181.20888523427 1159.07727742499 1140.78598141995 1125.46827162041 1112.46645460124 Columns 7 through 11 1101.2801646105 1091.52757665354 1082.91629725429 1075.22150683602 1068.26953695826
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 Comment
Image Analyst
Image Analyst on 13 Oct 2024
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.

Sign in to comment.


Star Strider
Star Strider on 13 Oct 2024
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)
1.0e+03 * 6.4478 0.4030 0.4008 0.4008 0.4008 0.4008 0.4008 0.4008 0.4008 0.4008 0.4008
disp(Im6)
1.0e+03 * 1.2083 1.1812 1.1591 1.1408 1.1255 1.1125 1.1013 1.0915 1.0829 1.0752 1.0683
plot(Im1,'LineWidth',2);
hold on
plot(Im6,'Linewidth',2)
.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!