# Only graphing two out of three of my graphs

3 views (last 30 days)
Caitlin Schmidt on 17 Sep 2021
Answered: Walter Roberson on 17 Sep 2021
I am trying to get three different data sets graphed on the same plot. When I hit run, only two out of three of the graphs are present. Is there something wrong in my code?
ch0 = ex32excel(:,2);
ch1 = ex32excel(:,4);
N = length(ch0);
deltast = 2e-5;
t1 = 0:deltast:deltast*(N-1);
xlow=0.02394;
zeropos=find(t1==xlow);
tnew=t1-xlow;
filename1 = 'Users/caitlin/Documents/MATLAB/PreLab3data.csv'
voltage1 = data(:,1);
time1 = data(:,2);
syms f(t);
f(t) = exp(-t/0.005);
x=linspace(0,10,100);
y=f(x);
plot(t1(zeropos:N),ch1(zeropos:N));
hold on
plot(voltage1,time1);
hold on
plot(y);
hold on

Chunru on 17 Sep 2021
ch0 = ex32excel(:,2);
ch1 = ex32excel(:,4);
N = length(ch0);
deltast = 2e-5;
t1 = 0:deltast:deltast*(N-1);
xlow=0.02394;
zeropos=find(t1==xlow);
tnew=t1-xlow;
filename1 = 'Users/caitlin/Documents/MATLAB/PreLab3data.csv'
voltage1 = data(:,1);
time1 = data(:,2);
% Pay attention to your function here. It would be veral small value
% except for 1st point at x=0.
syms f(t);
f(t) = exp(-t/0.005); % -t*.0005 ?
x=linspace(0,10,100);
y=f(x);
% Plot them in subplot (since x axis are very different)
subplot(311)
plot(t1(zeropos:N),ch1(zeropos:N));
subplot(312)
plot(voltage1,time1);
subplot(313)
plot(x, y);
Chunru on 17 Sep 2021
You need to find out the ranges of x and y for three sets of data in order to see how they will show up in one plot. We don't have your data and has no idea what the ranges are.
You can either post your executable code with data or show the subplots before we can help you out.
For the code segment above, you may want to try
zeropos=find(t1==xlow, 1, 'first'); % find the first entry.

Walter Roberson on 17 Sep 2021
N = 1200; %arbitrary
deltast = 2e-5;
t1 = 0:deltast:deltast*(N-1);
xlow=0.02394;
zeropos=find(t1==xlow)
zeropos = 1×0 empty double row vector
Empty. No exact matches.
[found, idx] = ismembertol(xlow, t1)
found = logical
1
idx = 1198
There is only a "close" value if N is at least 1198.
[mindiff, idx] = min(abs(t1-xlow))
mindiff = 3.4694e-18
idx = 1198
t1(idx) - xlow
ans = 3.4694e-18
Though it is pretty close.
Remember that the == operator between double precision numbers is looking for bit-for-bit identical values (exception: -0 will compare equal to 0). Two values that differ in their final bit will not == to be equal.
When you use the colon operator : with values that are nice fractions of a decimal, like 1e-5 = 1/100000 then you need to take into account that MATLAB (and nearly all computers) use double precision binary numbers, which are not able to exactly represent 1/10 or any negative power of 10.
This is just the same way that no finite decimal representation is able to exactly represent 1/3. You might represent 1/3 as 0.33333 decimal, but multiply that by 3 and you get 0.99999 decimal, which is not the same as 1.0 . Same problem if you use 0.333333333333333 -- multiply by 3 and you get a bunch of .9 that is not exactly 1.0
Double precision numbers are binary, using the sum of 1/2, 1/4, 1/8, 1/16, and so on. Stop at any finite number of terms and the result is not going to be exactly 1/10 .
When you use the colon operator, all those round-off errors accumulate. 2e-5 is not exactly represented . The inexact representation of 2e-5 is added to that, but the result may have twice the error that you would get if you had written 4e-5 directly..
Never use == for comparing floating point numbers unless you are certain the two numbers are drawn from the same source. For example it is fine to ask
A = rand(1,500);
minA = min(A)
find(A == minA)
the result of the min() will be a bit-for-bit identical copy of some element in A [except: technical details of NaN], and it is fine to use == to do bit-for-bit comparisons when you know that minA is going to be a bit-for-bit copy.