Hello, I need to interpolate two data. One is temperature and other is CO2. I need to interpolate it on same time interval and want to see the value of both (whose value is higher than other). I have tried interp1 function and I have been through many questions and answers here but couldn't find related. I have attached my code here. I always got the error of non monotonic increasing. I have a data from last thousand of years till 2010. In that temperature and CO2 has increased and decreased in some intervals and I can't reshape as it affects on final result. If anyone can help I would say please. I
clear all
load CO2data % Historical CO2 data from Harris
ageCO2=CO2data.years;
fCO2=CO2data.fCO2;
timeCO2=1950-ageCO2;
size(timeCO2)
figure(101)
plot(timeCO2/1000,fCO2)
xlabel('time (1000 years)')
ylabel('CO2 level (ppm)')
title('Historical CO2 level in the atmosphere (ppm)')
load Tdata % Historical temp data from Harris
ageT=Tdata.years;
Temp=Tdata.T;
timeT=1950-ageT;
size(timeT)
figure(102)
plot(timeT/1000,Temp)
xlabel('time (1000 years)')
ylabel('Temperature anomaly (C)')
title('Historical temperature anomaly (C)')
figure(103)
T=plotyy(timeCO2/1000,fCO2,timeT/1000,Temp);
xlabel('time ( 1000 years)')
ylabel(T(1),'CO2 level (ppm)')
ylabel(T(2),'historic temperature level(c)')
title('Atmospheric CO2 vs Temperature level')
u=interp1(fCO2,timeCO2/1000,280);
new_fCO2=linspace(280,191,length(fCO2));
new_timeCO2=interp1(fCO2,timeCO2/1000,new_fCO2,'spline');
figure(102)
plot(fCO2,timeCO2/1000,new_fCO2,new_timeCO2,'-o')

3 commentaires

KSSV
KSSV le 8 Déc 2020
What problem you are facing now?
TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 8 Déc 2020
Error in interp1 (line 161)
F = griddedInterpolant(X,V,method);
Error in CO2interpolation (line 16)
u=interp1(fCO2,timeCO2/1000,280);
TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 8 Déc 2020
Is there any way or any function I can interpolate CO2 and temperature with respect to time? Any command or function syntax?

Connectez-vous pour commenter.

 Réponse acceptée

Bjorn Gustavsson
Bjorn Gustavsson le 8 Déc 2020
Modifié(e) : Bjorn Gustavsson le 9 Déc 2020
The interpolation-function does something else than what you seem to try. It takes one data-vector, D, taken at a discrete set of monotously (sp?) varying points, t, and calculates interpolatevalues of D at another set of points, ti. In your case you have your t as years, if I understand correctly, and some corresponding measure of CO2. you might reinterpolate that data to a regular "montly" value like this:
ti = linspace(min(timeCO2),max(timeCO2),12*round(max(timeCO2)-min(timeCO2)));
fCO2_new = interp1(timeCO2,fCO2,ti,'pchip');
That is how the interpolation-function works. If that is the operation you need is not clear from the code you posted.
HTH

8 commentaires

TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 8 Déc 2020
Let me try as you have suggest.
TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 8 Déc 2020
I got new error like this. I tried by my way but I do not know why it showing like this I am using 2015b version.
Expression or statement is incorrect--possibly unbalanced (, {, or [.
Bjorn Gustavsson
Bjorn Gustavsson le 8 Déc 2020
Fixed copy-and-paste typo.
TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 9 Déc 2020
Modifié(e) : TEJASHKUMAR PATEL le 9 Déc 2020
Thanks for your time. After some manipulation I get the new error of "attemp to execute script name as a file"
but somehow I solved it but again I am where I was. Got the new error like this,
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 161)
F = griddedInterpolant(X,V,method);
Error in nobealo (line 32)
fCO2_new = interp1(timeCO2,fCO2,ti,'pchip');
Bjorn Gustavsson
Bjorn Gustavsson le 9 Déc 2020
Bah, fixed another typo...
TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 9 Déc 2020
Still same. I have attached both temperature and CO2 data with that you can help me more precisly. Thanks a lot
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 161)
F = griddedInterpolant(X,V,method);
Error in nobealo (line 32)
fCO2_new = interp1(timeCO2,fCO2,ti,'pchip');
If you do:
timeCO2=1950-ageCO2;
subplot(2,2,1),plot(diff(timeT),'.')
subplot(2,2,2),plot(diff(timeT)==0,'.')
subplot(2,2,4),plot(diff(timeCO2)==0,'.')
subplot(2,2,3),plot(diff(timeCO2),'.')
You will see that two consecutive points in your CO2-data are from the same time. That has to be resolved somehow. This way you get the average CO2-value:
timeCO2=1950-ageCO2;
idtCO2 = find(diff(timeCO2)==0);
timeCO2(idtCO2) = timeCO2(idtCO2)/2 + timeCO2(idtCO2+1)/2; % Average
timeCO2(idtCO2+1) = []; % remove the second point from the data
fCO2=CO2data.fCO2; % Same procedure for fCO2:
fCO2(idtCO2) = fCO2(idtCO2)/2 + fCO2(idtCO2+1)/2; % Average
fCO2(idtCO2+1) = []; % Remove second point
That is then possible to re-interpolate, here to 100000-timesteps between the first and last time in either data-set:
fCO2_reinterpolated = interp1(timeCO2,fCO2,linspace(min(min(timeCO2),min(timeT)),max(max(timeCO2),max(timeT)),1e5),'pchip');
HTH
TEJASHKUMAR PATEL
TEJASHKUMAR PATEL le 9 Déc 2020
Hey, above solution almost solved all of my problem regarding interpolation of non monotonic incresing data. Thank you very much. Now I will look into this little bit more in deep...

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by