7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Esi Tesi am 4 Apr. 2024
Bearbeitet: Torsten am 5 Apr. 2024
In MATLAB Online öffnen
I hve a set of x-y data such this:
dis0 = []; force0 = [];
which are for example 38 by 1 vectors and I want to interpolate force0 to get its new values on another x values like u_test which is a 100 by 1 vector using interp1 like this:
force0 = interp1(dis0,force0,u_test,'cubic')
but gives me the result like this:
I've tried all methods of interp1 like linear, cubic, makima, etc but could not get what I want. Any suggestions and solutions? How can I interpolate my curve through some other x values without losing its original points?
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Antworten (1)
John D'Errico am 4 Apr. 2024
Bearbeitet: John D'Errico am 4 Apr. 2024
In MATLAB Online öffnen
interp1 does not fail here. It merely fails to do what it was not programmed to do! Interp1 does as designed. That you try to use it in a way that is meaningless in context of what interp1 is written to do, then what could you possibly expect? This is NOT a question of linear versis cubic, or a spline! This is merely that interp1 is designed to interpolate a function that has only 1 value for any given value of x. Mathematically, interp1 assumes the mapping it will interpolate is a single valued one. So for ANY value of x, it will expect only one value for y, if that presumption fails, then you get garbage.
You have 31 points. You don't supply them, so I'll make some up.
t = linspace(1,2*pi,50);
x = cos(t);
y = sin(2*t);
plot(x,y,'o')
Clearly that is not a single valued mapping. You CANNOT just throw it at interp1 directly, not and hope interp1 will succeed. How can you interpolate it? The simplest way is to do it parametrically. That will mean to interpolate x and y SEPARATELY and independently.
I'll use a linear interpolant. Be careful if you try to use a spline interpolant. Then you would need to be more creative about how to build that parametric vector s. But you can be quite free in how you build the vector sinterp as long as it is merely linear interpolation that is desired, and the data I saw from you does not merit more than a linear interpolation.
s = 1:numel(x);
ninterp = 1000; % 1000 points along the curve
sinterp = linspace(1,numel(x),ninterp);
xinterp = interp1(s,x,sinterp,'linear');
yinterp = interp1(s,y,sinterp,'linear');
plot(x,y,'ro',xinterp,yinterp,'b-')
Again, if you want a smooth interpolant like a spline, a tool like cscvn is recommended. Or you could use my interparc utility (as found on the file exchange for free download.)
4 Kommentare 2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Esi Tesi am 4 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2103106-interp1-fails-to-interpolate-on-a-non-one-to-one-function-curve#comment_3121616
⋮
Bearbeitet: Esi Tesi am 4 Apr. 2024
- example.m
Thank you for your full explanation.
But if I've undertstood your idea correctly, your method tries to interpolate for a specific number of constant intervals (like 1000 that you just did. Also interparc does the same as far as I know) which is not suitable for my work.
I want to find force0 values on another x coordinates which are known values. (example file is attached.)
Please correct me if I'm wrong.
Torsten am 4 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2103106-interp1-fails-to-interpolate-on-a-non-one-to-one-function-curve#comment_3121661
Bearbeitet: Torsten am 4 Apr. 2024
In MATLAB Online öffnen
The blue curve is the curve how MATLAB's "interp1" interprets your data curve (dis0,force0).
The red dots are the interpolated values force0 for the vector u_test.
So everything in order.
You see the problem ? Your original (dis0,force0) curve is not even a function - one x-value has several corresponding y-values. Thus from which branch of the curve (dis0,force0) should MATLAB's "interp1" return the value of force0 for a given u_test-value ? There are at least two, in some regions even three possible choices (just count the number of possible y-values corresponding to a given x-value)
clear all ;
dis0 = [0
0.0015
0.0030
0.0045
0.0061
0.0076
0.0098
0.0133
0.0184
0.0235
0.0286
0.0363
0.0439
0.0468
0.0314
0.0161
0.0046
-0.0069
-0.0184
-0.0299
-0.0414
-0.0472
-0.0360
-0.0346
-0.0332
-0.0318
-0.0304
-0.0283
-0.0252
-0.0205
-0.0134
-0.0063
0.0007
0.0113
0.0219
0.0325
0.0431
0.0500];
force0 = [0
34.8857
46.9174
47.9857
48.8694
49.7155
51.1528
53.1095
55.6937
57.5942
59.2924
61.8361
63.4537
63.1040
-67.3671
-71.6496
-73.5408
-75.7091
-77.6189
-79.3296
-80.9250
-80.6832
79.7957
78.7370
78.6545
78.5761
78.5055
78.6988
78.8633
79.0256
79.0934
78.5507
78.1071
78.0408
77.2298
76.5155
75.7774
74.7907];
u_test = [0
0.0002
0.0004
0.0005
0.0007
0.0009
0.0011
0.0012
0.0014
0.0016
0.0021
0.0028
0.0047
0.0098
0.0158
0.0235
0.0340
0.0466
0.0464
0.0462
0.0460
0.0458
0.0456
0.0454
0.0452
0.0450
0.0448
0.0446
0.0444
0.0442
0.0440
0.0438
0.0436
0.0433
0.0431
0.0429
0.0427
0.0425
0.0423
0.0421
0.0419
0.0417
0.0415
0.0413
0.0411
0.0409
0.0407
0.0405
0.0403
0.0401
0.0399
0.0333
0.0242
0.0050
-0.0175
-0.0420
-0.0498
-0.0496
-0.0494
-0.0493
-0.0491
-0.0490
-0.0488
-0.0486
-0.0485
-0.0483
-0.0482
-0.0480
-0.0479
-0.0477
-0.0475
-0.0474
-0.0472
-0.0471
-0.0469
-0.0467
-0.0466
-0.0463
-0.0461
-0.0458
-0.0456
-0.0453
-0.0451
-0.0448
-0.0446
-0.0443
-0.0440
-0.0438
-0.0435
-0.0433
-0.0430
-0.0428
-0.0425
-0.0422
-0.0420
-0.0417
-0.0415
-0.0412
-0.0021
0.0500
];
[dis0,idx] = sort(dis0);
force0 = force0(idx);
u_test = sort(u_test);
hold on
plot(dis0,force0,'b')
force0 = interp1(dis0,force0,u_test,'linear');
plot(u_test,force0,'or')
hold off
Esi Tesi am 5 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2103106-interp1-fails-to-interpolate-on-a-non-one-to-one-function-curve#comment_3121961
In MATLAB Online öffnen
Yes, but however my curve has to look like the first curve that I shared above. (original curve with (dis0,force0))
If that's the problem, we can add a small decimal to force0 vector like this:
force0 = c*msum(ones(size(force0)))*eps + force0;
but this does not fix it and I still can't find force0 for corresponding u_test values.
Torsten am 5 Apr. 2024
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/2103106-interp1-fails-to-interpolate-on-a-non-one-to-one-function-curve#comment_3122051
Bearbeitet: Torsten am 5 Apr. 2024
Yes, but however my curve has to look like the first curve that I shared above.
This is the first curve that you shared. The values of dis0 are only sorted. If you now sort force0 accordingly and connect the points, you get the blue curve. And this is the curve that interp1 uses for interpolation.
Look at your first curve that you shared.
Say u_test = 0.02.
Don't you understand that interp1 does not know whether to return -80, 60 or 80 because there are three possible values for force0 ?
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABMathematicsInterpolation
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Tags
- interp1
- interpolate
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung