Only graphing two out of three of my graphs

조회 수: 7 (최근 30일)
Caitlin Schmidt
Caitlin Schmidt 2021년 9월 17일
답변: Walter Roberson 2021년 9월 17일
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'
data1=csvread(filename1,1,0);
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

답변 (2개)

Chunru
Chunru 2021년 9월 17일
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'
data1=csvread(filename1,1,0);
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);
  댓글 수: 4
Caitlin Schmidt
Caitlin Schmidt 2021년 9월 17일
I've actually been able to narrow down my issue to the portion down below:
xlow=0.02394;
zeropos=find(t1==xlow);
tnew=t1-xlow;
If I remove this section, I get all three curves on the same graph, I just don't have the curves all aligned at 0.
Chunru
Chunru 2021년 9월 17일
편집: Chunru 2021년 9월 17일
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
Walter Roberson 2021년 9월 17일
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.

카테고리

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