error from Interpolation of the attached data

could you please help me debug Matlab code for interpolation of the attached data?
Thanks,
load('data.mat');
figure(1);plot(x1,y1);
figure(2);plot(x2,y2);
assert(numel(unique(x1))==numel(x1),'Replicated point(s) in x1')
%assert(numel(unique(x2))==numel(x2),'Replicated point(s) in x2')
As the error message you got says, there is a duplicated value in the x2 vector -- not allowed in interp1
ix=find(diff(x2)==0) % find who are the culprits
ix = 56
format longe
x2(ix:ix+1)
ans = 2×1
1.052958067000000e+03 1.052958067000000e+03
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[x2,ia]=unique(x2);
y2=y2(ia);
x= linspace(700,1400,701).';
y11 = interp1(x1,y1,x);
y22 = interp1(x2,y2,x);
figure(3);plot(x,y11);
figure(4);plot(x,y22);

 Réponse acceptée

clear all
load('data.mat');
[x1,i1] = unique(x1);
y1 = y1(i1);
[x2,i2] = unique(x2);
y2 = y2(i2);
figure(1);plot(x1,y1);
figure(2);plot(x2,y2);
x= linspace(700,1400,701).';
y11 = interp1(x1,y1,x);
y22 = interp1(x2,y2,x);
figure(3);plot(x,y11);
figure(4);plot(x,y22);

Plus de réponses (1)

Catégories

Question posée :

JK
le 27 Mai 2026 à 18:42

Réponse apportée :

Jan
le 28 Mai 2026 à 3:20

Community Treasure Hunt

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

Start Hunting!

Translated by