How can I calculate the area under a graph (Shaded area)?
15 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to calculate the area of the shaded area shown in the graph. This is my Matlab code. When I use trapz keyword it gives me the whole area. Is there any way to calculate the shaded area 1 and area 2 which are separated by the dotted lines.
clear all
clc
close all
x=0:0.1:3.14;
y=sin(4*x).*sec(3*x);
yy = zeros(1,length(x));
yy(:) = 0;
y1=-20:0.1:20;
x1 = ones(1,length(y1));
x1(:) = 0.5;
plot(x,y,x,yy,'--',x1,y1,'--')
trapx(x,y)
Is there also any way to select area 1 and paint it with different colors using matlab code.
Thanks
采纳的回答
Star Strider
2016-11-29
Try this:
x=0:0.1:3.14;
y=sin(4*x).*sec(3*x);
yy = zeros(1,length(x));
yy(:) = 0;
y1=-20:0.1:20;
x1 = ones(1,length(y1));
x1(:) = 0.5;
area_1 = trapz(x(x<=0.5), y(x<=0.5)); % Calculate ‘Area #1’
xc = find((y .* circshift(y, [0 1])) < 0); % Define ‘y’ Zero-Crossings
idx_rng = xc(1)-1:xc(1); % Index Range Including Zero-Crossing
x0 = interp1(y(xc(1)-1:xc(1)), x(xc(1)-1:xc(1)), 0); % Find ‘x’ At Zero Crossing
area_2 = trapz([0.5 x0], [y(x == 0.5) 0]); % Calculate ‘Area #2’
plot(x,y,x,yy,'--',x1,y1,'--')
hold on
ha1 = area(x(x<=0.5), y(x<=0.5), 'FaceColor','g');
ha2 = area([0.5 x0], [y(x == 0.5) 0], 'FaceColor','r');
hold off
A1str = sprintf('Area 1 = %6.3f', area_1);
A2str = sprintf('Area 2 = %6.3f', area_2);
legend([ha1 ha2], A1str, A2str)
I colored ‘area2’ for clarity, and to show the value in the legend. Make whatever changes you need to. Note that this code is specific to your Question as you posted it, and while the ideas will generalise to similar applications, the code itself will not.
14 个评论
friet
2016-12-9
Thank you very much
clear all
x=0:0.1:3.14;
y=sin(4*x).*sec(3*x);
yy = zeros(1,length(x));
yy(:) = 0;
y1=-20:0.1:20;
x1 = ones(1,length(y1));
x1(:) = 0.5;
area_1 = trapz(x(x<=0.5), y(x<=0.5)); % Calculate ‘Area #1’
xc = find((y .* circshift(y, [0 1])) < 0); % Define ‘y’ Zero-Crossings
idx_rng = xc(1)-1:xc(1); % Index Range Including Zero-Crossing
x0 = interp1(y(xc(1)-1:xc(1)), x(xc(1)-1:xc(1)), 0); % Find ‘x’ At Zero Crossing
area_2 = trapz([0.5 x0], [y(x == 0.5) 0]); % Calculate ‘Area #2’
plot(x,y,x,yy,'--',x1,y1,'--')
hold on
ha1 = area(x(x<=0.5), y(x<=0.5), 'FaceColor','g');
ha2 = area([0.5 x0], [y(x == 0.5) 0], 'FaceColor','r');
hold off
A1str = sprintf('Area 1 = %6.3f', area_1);
A2str = sprintf('Area 2 = %6.3f', area_2);
legend([ha1 ha2], A1str, A2str)
x01 = interp1(y(xc(2)-1:xc(2)), x(xc(2)-1:xc(2)), 0);
area_3= trapz([0.5812 x01], [y(x == 0.5) 1]);
figure(3)
plot(x,y,x,yy,'--',x1,y1,'--')
hold on
ha1 = area(x(x<=0.5), y(x<=0.5), 'FaceColor','g');
ha2 = area([0.5 x0], [y(x == 0.5) 0], 'FaceColor','r');
ha3=area([0.5812 x01], [y(x == 0.5) 0], 'FaceColor','b');
hold off
A1str = sprintf('Area 1 = %6.3f', area_1);
A2str = sprintf('Area 2 = %6.3f', area_2);
A3str = sprintf('Area 3 = %6.3f', area_3);
legend([ha1 ha2 ha3], A1str, A2str, A3str)
I am continuing to work to find area 3, as shown in the figure.
I am able to trace the zero crossing in x axis however I cant fins the right data points on the y-axis. Can you help me. Thank you!!
Star Strider
2016-12-10
My pleasure!
The y-values at the zero-crossings are by definition zero. You only really need the zero-crossing x-values to calculate the integrals of the segments between the zero-crossings. Use the trapz function for those.
A little utility function I wrote to find the approximate zero-crossing indices may be helpful:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
Use this with your y-data vector.
Star Strider
2016-12-10
I couldn’t figure out your ‘area_3’ calculation, so I changed the code just a bit to make the calculations easier:
x=0:0.1:3.14;
yf = @(x) sin(4*x).*sec(3*x); % Create Anonymous Function
y = yf(x);
yy = zeros(1,length(x));
yy(:) = 0;
y1=-20:0.1:20;
x1 = ones(1,length(y1));
x1(:) = 0.5;
area_1 = trapz(x(x<=0.5), y(x<=0.5)); % Calculate ‘Area #1’
xc = find((y .* circshift(y, [0 1])) < 0); % Define ‘y’ Zero-Crossings
idx_rng = xc(1)-1:xc(1); % Index Range Including Zero-Crossing
x0 = interp1(y(xc(1)-1:xc(1)), x(xc(1)-1:xc(1)), 0); % Find ‘x’ At Zero Crossing
area_2 = trapz([0.5 x0], [y(x == 0.5) 0]); % Calculate ‘Area #2’
for k1 = 1:length(xc)
ix_rng = xc(k1)-1:xc(k1); % Index Range
x0v(k1) = interp1(y(ix_rng), x(ix_rng), 0); % Interpolate
end
for k1 = 1:length(x0v)-1
area_(k1) = integral(yf, x0v(k1), x0v(k1+1)); % Integrate Using ‘integral’
end
plot(x,y,x,yy,'--',x1,y1,'--')
hold on
ha1 = area(x(x<=0.5), y(x<=0.5), 'FaceColor','g');
ha2 = area([0.5 x0], [y(x == 0.5) 0], 'FaceColor','r');
hold off
A1str = sprintf('Area 1 = %6.3f', area_1);
A2str = sprintf('Area 2 = %6.3f', area_2);
legend([ha1 ha2], A1str, A2str)
The changes aren’t plotted. The ‘area_()’ vector contains in order: ‘area_3’, ‘area_4’, and ‘area_5’+‘area_6’. You will have to insert the line that separates the last two in order to calculate the integrals separately. (I don’t see it.) It should be inserted just before the last element that I calculate in ‘x0v’. You will probably have to do that manually (that is, specifically code for it). That is straightforward to do.
Example:
old_vector = 1:5;
new_vector([1:length(old_vector)-1 length(old_vector)+1]) = old_vector;
new_vector(end-1) = pi;
producing:
old_vector =
1 2 3 4 5
new_vector =
1 2 3 4 3.1416 5
Make the appropriate changes to do something similar for ‘x0v’.
friet
2016-12-11
Thank you very much I try to update the area, However I am getting something wrong. Can you please check my code below.
clear all
clc
% Create Anonymous Function
x=0:0.1:3.14;
yf = @(x) sin(4*x).*sec(3*x);
y = yf(x);
yy = zeros(1,length(x));
yy(:) = 0;
y1=-20:0.1:20;
x1 = ones(1,length(y1));
x1(:) = 0.5;
x2 = ones(1,length(y1));
x2(:) = 2.6;
figure(1)
plot(x,y,x,yy,x1,y1,'--',x2,y1,'--')
%Area calculation
area_1 = trapz(x(x<=0.5), y(x<=0.5)); % Calculate ‘Area #1’
% area_1= integral(yf, 0, 0.5); Why this dont work
xc = find((y .* circshift(y, [0 1])) < 0);
x0 = interp1(y(xc(1)-1:xc(1)), x(xc(1)-1:xc(1)), 0); % Find ‘x’ At Zero Crossing
area_2 = trapz([0.5 x0], [y(x == 0.5) 0]);
%
for k1 = 1:length(xc)
ix_rng = xc(k1)-1:xc(k1); % Index Range
x0v(k1) = interp1(y(ix_rng), x(ix_rng), 0); % Interpolate
end
%
area_3= integral(yf, x0v(1), x0v(2));
area_4= integral(yf, x0v(2), x0v(3));
area_5= integral(yf, x0v(3), 2.6);
area_6= integral(yf, 2.6, x0v(4));
%Postprocessing
figure(2)
plot(x,y,x,yy,'--',x1,y1,'--',x2,y1,'--')
hold on
hold on
ha1 = area(x(x<=0.5), y(x<=0.5), 'FaceColor','g');
ha2 = area([0.5 x0], [y(x == 0.5) 0], 'FaceColor','r');
ha3 = area([x0 x0v(2)],[y(x == 0.5) 0], 'FaceColor','b');
ha4 = area([x0v(1) x0v(2)],[y(x == x0v(2)) 0], 'FaceColor','k');
ha5 = area([x0v(3) 2.6],[y(x == x0v(3)) 0], 'FaceColor','y');
ha6 = area([2.6 x0v(4)],[y(x == x0v(3)) 0], 'FaceColor','c');
hold off
A1str = sprintf('Area 1 = %6.3f', area_1);
A2str = sprintf('Area 2 = %6.3f', area_2);
A3str = sprintf('Area 3 = %6.3f', area_3);
A4str = sprintf('Area 4 = %6.3f', area_4);
A5str = sprintf('Area 5 = %6.3f', area_5);
A6str = sprintf('Area 6 = %6.3f', area_6);
legend([ha1 ha2 ha3 ha4 ha5 ha6], A1str, A2str, A3str, A4str, A5str, A6str)
And other question i have is why
area_1= integral(yf, 0, 0.5);
|is not giving me the same answer as|
area_1 = trapz(x(x<=0.5), y(x<=0.5));
Star Strider
2016-12-11
My newest code doesn’t start at ‘area_1’. The loop starts at ‘area_3’. The values for ‘area_1’ and ‘area_2’ should be as they were before.
Star Strider
2016-12-11
It is very late here, so I decided to make this easier for me. You can change the resolution by changing the number of elements in ‘x’.
The Code —
N = 100; % Number Of Elements In ‘x’
x = linspace(0, pi);
yf = @(x) sin(4*x).*sec(3*x); % Create Anonymous Function
y = yf(x);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
xc = zci(y); % Zero-Crossings
[~,ixp] = findpeaks(y, 'MinPeakHeight',5); % Positive Peak Location Indices
[~,ixn] = findpeaks(-y); % Negative Peak Locations
ixs = unique([xc(:)',ixp(:)',ixn(:)']); % Sorted Indices (Ascending)
for k1 = 1:length(ixs)-1 % Loop Through Sorted Indices
area_(k1) = integral(yf, x(ixs(k1)), x(ixs(k1+1))); % Integrate Using ‘integral’
end
fprintf(1, '\n\tArea Segments —\n')
for k1 = 1:length(area_)
fprintf(1,'\t\tArea %d = %8.4f\n',k1, area_(k1)) % Print Area Segments
legstr{k1} = sprintf('Area %d = %8.4f\n',k1, area_(k1)); % Create Legend Strings
end
cm = colormap(jet(length(area_))); % Color Map For Patches
figure(1)
plot(x, y) % Plot Function
hold on
for k1 = 1:length(area_) % Plot Patches
ixv = [ixs(k1):ixs(k1+1)]; % Index Range
hp{k1} = patch([x(ixv) flip(x(ixv))], [yf(x(ixv)) zeros(size(flip(x(ixv))))], cm(k1,:));
end
legend([hp{:}], legstr, 'Location','N') % Legend
The patch calls are straightforward to code, but not easy to understand without reading the documentation and doing some experimentation. I will go into them in a bit of detail if you want.
The Plot —
I made some simplifications to make the code work efficiently. This does what you want without all the detailed coding originally required. You may have to enlarge the plot to see ‘Area 2’ and ‘Area 6’, both newly-defined in this code. They don’t correspond exactly to your original description, but the code works, and tonight that’s good enough for me. I comment-documented all but the patch call (that seems to be incidental anyway).
friet
2016-12-12
编辑:friet
2016-12-12
Thanks
What about when I have datapoints instead of arbitrary equation I try below, However the integral function dont work. Can you help me. Thanks
clear all
close all
clc
x=[-64.8138
-63.9828
-63.1519
-62.3209
-61.4900
-60.6590
-59.8281
-58.9971
-58.1662
-57.3352
-56.5043
-55.6734
-54.8424
-54.0115
-53.1805
-52.3496
-51.5186
-50.6877
-49.8567
-49.0258
-48.1948
-47.3639
-46.5330
-45.7020
-44.8711
-44.0401
-43.2092
-42.3782
-41.5473
-40.7163
-39.8854
-39.0544
-38.2235
-37.3926
-36.5616
-35.7307
-34.8997
-34.0688
-33.2378
-32.4069
-31.5759
-30.7450
-29.9140
-29.0831
-28.2521
-27.4212
-26.5903
-25.7593
-24.9284
-24.0974
-23.2665
-22.4355
-21.6046
-20.7736
-19.9427
-19.1117
-18.2808
-17.4499
-16.6189
-15.7880
-14.9570
-14.1261
-13.2951
-12.4642
-11.6332
-10.8023
-9.9713
-9.1404
-8.3095
-7.4785
-6.6476
-5.8166
-4.9857
-4.1547
-3.3238
-2.4928
-1.6619
-0.8309
0.0000
0.8309
1.6619
2.4928
3.3238
4.1547
4.9857
5.8166
6.6476
7.4785
8.3095
9.1404
9.9713
10.8023
11.6332
12.4642
13.2951
14.1261
14.9570
15.7880
16.6189
17.4499
18.2808
19.1117
19.9427
20.7736
21.6046
22.4355
23.2665
24.0974
24.9284
25.7593
26.5903
27.4212
28.2521
29.0831
29.9140
30.7450
31.5759
32.4069
33.2378
34.0688
34.8997
35.7307
36.5616
37.3926
38.2235
39.0544
39.8854
40.7163
41.5473
42.3782
43.2092
44.0401
44.8711
45.7020
46.5330
47.3639
48.1948
49.0258
49.8567
50.6877
51.5186
52.3496
53.1805
54.0115
54.8424
55.6734
56.5043
57.3352
58.1662
58.9971
59.8281
60.6590
61.4900
62.3209
63.1519
63.9828
64.8138
65.6447
66.4756
67.3066
68.1375
68.9685
69.7994
70.6304
71.4613
72.2923
73.1232
73.9542
74.7851
75.6160
76.4470
77.2779
78.1089
78.9398
79.7708
80.6017
81.4327
82.2636
83.0946
83.9255
84.7564
85.5874
86.4183
87.2493
88.0802
88.9112
89.7421
90.5731
91.4040
92.2350
93.0659
93.8968
94.7278
95.5587
96.3897
97.2206
98.0516
98.8825
99.7135
100.5444
101.3754
102.2063
103.0372
103.8682
104.6991
105.5301
106.3610
107.1920
108.0229
108.8539
109.6848
110.5158
111.3467
112.1777
113.0086
113.8395
114.6705
115.5014
116.3324
117.1633
117.9943
118.8252
119.6562
120.4871
121.3181
122.1490
122.9799
123.8109
124.6418
125.4728
126.3037
127.1347
127.9656
128.7966
129.6275
130.4585
131.2894
132.1203
132.9513
133.7822
134.6132
135.4441
136.2751
137.1060
137.9370
138.7679
139.5989
140.4298
141.2607
142.0917
142.9226
143.7536
144.5845
145.4155
146.2464
147.0774
147.9083
148.7393
149.5702
150.4011
151.2321
152.0630
152.8940
153.7249
154.5559
155.3868
156.2178
157.0487
157.8797
158.7106
159.5415
160.3725
161.2034
162.0344
162.8653
163.6963
164.5272
165.3582
166.1891
167.0201
167.8510
168.6819
169.5129
170.3438
171.1748
172.0057
172.8367
173.6676
174.4986
175.3295
176.1605
176.9914
177.8223
178.6533
179.4842
180.3152
181.1461
181.9771
182.8080
183.6390
184.4699
185.3009
186.1318
186.9628
187.7937
188.6246
189.4556
190.2865
191.1175
191.9484
192.7794
193.6103
194.4413
195.2722
196.1032
196.9341
197.7650
198.5960
199.4269
200.2579
201.0888
201.9198
202.7507
203.5817
204.4126
205.2436
206.0745
206.9054
207.7364
208.5673
209.3983
210.2292
211.0602
211.8911
212.7221
213.5530
214.3840
215.2149
216.0458
216.8768
217.7077
218.5387
219.3696
220.2006
221.0315
221.8625
222.6934
223.5244
224.3553
225.1862
226.0172
226.8481
227.6791
228.5100
229.3410
230.1719
231.0029
231.8338
232.6648
233.4957
234.3266
235.1576
235.9885
236.8195
237.6504
238.4814
239.3123
240.1433
240.9742
241.8052
242.6361
243.4670
244.2980
245.1289
245.9599
246.7908
247.6218
248.4527
249.2837
250.1146
250.9456
251.7765
252.6074
253.4384
254.2693
255.1003
255.9312
256.7622
257.5931
258.4241
259.2550
260.0860
260.9169
261.7479
262.5788
263.4097
264.2407
265.0716
265.9026
266.7335
267.5645
268.3954
269.2264
270.0573
270.8883
271.7192
272.5501
273.3811
274.2120
275.0430
275.8739
276.7049
277.5358
278.3668
279.1977
280.0287
280.8596
281.6905
282.5215
283.3524
284.1834
285.0143
285.8453
286.6762
287.5072
288.3381
289.1691
290.0000
290.8309
291.6619
292.4928
293.3238
294.1547
294.9857
295.8166
296.6476
297.4785
298.3095
299.1404
299.9713
300.8023
301.6332
302.4642
303.2951
304.1261
304.9570
305.7880
306.6189
307.4499
308.2808
309.1117
309.9427
310.7736
311.6046
312.4355
313.2665
314.0974
314.9284
315.7593
316.5903
317.4212
318.2521
319.0831
319.9140
320.7450
321.5759
322.4069
323.2378
324.0688
324.8997
325.7307
326.5616
327.3926
328.2235
329.0544
329.8854
330.7163
331.5473
332.3782
333.2092
334.0401
334.8711
335.7020
336.5330
337.3639
338.1948
339.0258
339.8567
340.6877
341.5186
342.3496
343.1805
344.0115
344.8424
345.6734
346.5043
347.3352
348.1662
348.9971
349.8281
350.6590
351.4900
352.3209
353.1519
353.9828
354.8138
355.6447
356.4756
357.3066
358.1375
358.9685
359.7994
360.6304
361.4613
362.2923
363.1232
363.9542
364.7851
365.6160
366.4470
367.2779
368.1089
368.9398
369.7708
370.6017
371.4327
372.2636
373.0946
373.9255
374.7564
375.5874
376.4183
377.2493
378.0802
378.9112
379.7421
380.5731
381.4040
382.2350
383.0659
383.8968
384.7278
385.5587
386.3897
387.2206
388.0516
388.8825
389.7135
390.5444
391.3754
392.2063
393.0372
393.8682
394.6991
395.5301
396.3610
397.1920
398.0229
398.8539
399.6848
400.5158
401.3467
402.1777
403.0086
403.8395
404.6705
405.5014
406.3324
407.1633
407.9943
408.8252
409.6562
410.4871
411.3181
412.1490
412.9799
413.8109
414.6418
415.4728
416.3037
417.1347
417.9656
418.7966
419.6275
420.4585
421.2894
422.1203
422.9513
423.7822
424.6132
425.4441
426.2751
427.1060
427.9370
428.7679
429.5989
430.4298
431.2607
432.0917
432.9226
433.7536];
sf=@(x) [-0.9243
-0.9691
-1.0153
-1.0631
-1.1128
-1.1643
-1.2177
-1.2737
-1.3319
-1.3929
-1.4565
-1.5231
-1.5929
-1.6660
-1.7428
-1.8232
-1.9077
-1.9964
-2.0897
-2.1877
-2.2911
-2.3997
-2.5142
-2.6347
-2.7618
-2.8959
-3.0374
-3.1867
-3.3442
-3.5106
-3.6859
-3.8710
-4.0661
-4.2720
-4.4887
-4.7168
-4.9566
-5.2085
-5.4726
-5.7493
-6.0386
-6.3404
-6.6548
-6.9815
-7.3202
-7.6704
-8.0317
-8.4033
-8.7842
-9.1735
-9.5699
-9.9720
-10.3784
-10.7875
-11.1974
-11.6061
-12.0117
-12.4117
-12.8042
-13.1866
-13.5565
-13.9116
-14.2493
-14.5672
-14.8629
-15.1343
-15.3791
-15.5953
-15.7808
-15.9342
-16.0538
-16.1385
-16.1873
-16.1994
-16.1744
-16.1123
-16.0131
-15.8773
-15.7057
-15.4992
-15.2592
-14.9872
-14.6851
-14.3549
-13.9987
-13.6190
-13.2182
-12.7990
-12.3640
-11.9160
-11.4574
-10.9914
-10.5203
-10.0469
-9.5734
-9.1022
-8.6357
-8.1756
-7.7240
-7.2825
-6.8525
-6.4355
-6.0323
-5.6439
-5.2710
-4.9141
-4.5736
-4.2497
-3.9421
-3.6512
-3.3762
-3.1173
-2.8737
-2.6451
-2.4308
-2.2302
-2.0426
-1.8673
-1.7037
-1.5511
-1.4088
-1.2760
-1.1523
-1.0367
-0.9289
-0.8282
-0.7343
-0.6465
-0.5646
-0.4878
-0.4162
-0.3492
-0.2865
-0.2282
-0.1736
-0.1230
-0.0757
-0.0320
8.5842e-3
0.0459
0.0802
0.1116
0.1402
0.1661
0.1895
0.2106
0.2294
0.2461
0.2608
0.2738
0.2853
0.2955
0.3045
0.3128
0.3204
0.3276
0.3348
0.3421
0.3500
0.3586
0.3683
0.3791
0.3914
0.4056
0.4217
0.4399
0.4605
0.4834
0.5089
0.5371
0.5679
0.6013
0.6375
0.6762
0.7174
0.7609
0.8066
0.8543
0.9037
0.9546
1.0066
1.0595
1.1130
1.1666
1.2203
1.2735
1.3258
1.3769
1.4265
1.4744
1.5200
1.5632
1.6038
1.6413
1.6757
1.7066
1.7339
1.7574
1.7772
1.7930
1.8048
1.8125
1.8162
1.8160
1.8117
1.8037
1.7916
1.7760
1.7568
1.7343
1.7083
1.6795
1.6477
1.6133
1.5763
1.5369
1.4957
1.4524
1.4076
1.3612
1.3135
1.2647
1.2150
1.1644
1.1133
1.0618
1.0098
0.9578
0.9055
0.8534
0.8013
0.7495
0.6978
0.6465
0.5955
0.5449
0.4948
0.4450
0.3959
0.3472
0.2991
0.2513
0.2042
0.1575
0.1114
0.0657
0.0206
-0.0239
-0.0680
-0.1115
-0.1545
-0.1968
-0.2386
-0.2796
-0.3201
-0.3598
-0.3988
-0.4370
-0.4743
-0.5108
-0.5462
-0.5807
-0.6141
-0.6463
-0.6773
-0.7071
-0.7354
-0.7623
-0.7877
-0.8116
-0.8339
-0.8545
-0.8733
-0.8904
-0.9057
-0.9192
-0.9308
-0.9405
-0.9484
-0.9544
-0.9585
-0.9609
-0.9614
-0.9602
-0.9574
-0.9529
-0.9468
-0.9392
-0.9303
-0.9202
-0.9088
-0.8965
-0.8831
-0.8689
-0.8541
-0.8385
-0.8226
-0.8063
-0.7898
-0.7731
-0.7565
-0.7399
-0.7235
-0.7075
-0.6918
-0.6766
-0.6618
-0.6476
-0.6341
-0.6213
-0.6094
-0.5981
-0.5877
-0.5780
-0.5692
-0.5612
-0.5541
-0.5479
-0.5425
-0.5380
-0.5343
-0.5315
-0.5293
-0.5281
-0.5277
-0.5281
-0.5290
-0.5305
-0.5328
-0.5356
-0.5390
-0.5427
-0.5468
-0.5511
-0.5556
-0.5599
-0.5643
-0.5685
-0.5722
-0.5754
-0.5776
-0.5790
-0.5792
-0.5781
-0.5752
-0.5705
-0.5636
-0.5545
-0.5426
-0.5279
-0.5102
-0.4890
-0.4642
-0.4353
-0.4024
-0.3650
-0.3231
-0.2761
-0.2240
-0.1665
-0.1032
-0.0339
0.0416
0.1235
0.2123
0.3082
0.4117
0.5229
0.6423
0.7701
0.9069
1.0531
1.2091
1.3754
1.5525
1.7408
1.9408
2.1532
2.3783
2.6169
2.8694
3.1363
3.4181
3.7151
4.0278
4.3567
4.7020
5.0638
5.4423
5.8376
6.2494
6.6776
7.1217
7.5813
8.0557
8.5441
9.0453
9.5581
10.0811
10.6127
11.1511
11.6943
12.2400
12.7860
13.3296
13.8683
14.3993
14.9196
15.4263
15.9164
16.3865
16.8340
17.2554
17.6480
18.0089
18.3354
18.6250
18.8752
19.0839
19.2495
19.3703
19.4449
19.4727
19.4529
19.3855
19.2705
19.1086
18.9006
18.6479
18.3519
18.0146
17.6383
17.2253
16.7785
16.3008
15.7953
15.2652
14.7139
14.1447
13.5611
12.9667
12.3647
11.7583
11.1509
10.5454
9.9446
9.3514
8.7682
8.1971
7.6404
7.0996
6.5764
6.0722
5.5879
5.1245
4.6824
4.2621
3.8639
3.4877
3.1335
2.8007
2.4892
2.1982
1.9272
1.6753
1.4415
1.2254
1.0256
0.8415
0.6718
0.5158
0.3724
0.2408
0.1197
8.6221e-3
-0.0934
-0.1874
-0.2739
-0.3538
-0.4278
-0.4964
-0.5603
-0.6200
-0.6758
-0.7284
-0.7780
-0.8253
-0.8702
-0.9131
-0.9542
-0.9936
-1.0318
-1.0685
-1.1041
-1.1384
-1.1716
-1.2036
-1.2346
-1.2643
-1.2929
-1.3202
-1.3461
-1.3705
-1.3935
-1.4149
-1.4346
-1.4525
-1.4685
-1.4826
-1.4945
-1.5043
-1.5118
-1.5169
-1.5197
-1.5200
-1.5178
-1.5129
-1.5056
-1.4955
-1.4829
-1.4677
-1.4499
-1.4295
-1.4066
-1.3813
-1.3537
-1.3238
-1.2916
-1.2575
-1.2214
-1.1834
-1.1438
-1.1027
-1.0602
-1.0165
-0.9718
-0.9261
-0.8799
-0.8330
-0.7858
-0.7385
-0.6912
-0.6440
-0.5971
-0.5509
-0.5053
-0.4608
-0.4172
-0.3748
-0.3337
-0.2941
-0.2563
-0.2202
-0.1860
-0.1538
-0.1238
-0.0959
-0.0703
-0.0472
-0.0265
-8.4767e-3
7.1236e-3
0.0200
0.0304
0.0380
0.0431
0.0453
0.0450
0.0422
0.0368
0.0288
0.0184
5.8358e-3
-9.1550e-3
-0.0262
-0.0454
-0.0664
-0.0891
-0.1135
-0.1396
-0.1670
-0.1956
-0.2254
-0.2563
-0.2881
-0.3206
-0.3541
-0.3880
-0.4226
-0.4576
-0.4932
-0.5292
-0.5658
-0.6029
-0.6404
-0.6785
-0.7172
-0.7567
-0.7967
-0.8380
-0.8802];
s = sf(x);
yy = zeros(1,length(x));
yy(:) = 0;
y1=-20:0.0666:20;
x1 = ones(1,length(y1));
x1(:) = 0;
y12=-20:0.0666:20;
x12 = ones(1,length(y1));
x12(:) = 290;
plot(x,s,x,yy,'--',x1,y1,'--',x12,y12,'--')
xc=[79 139,248,363,428]; % intercept points
Area1=integral(sf,xc(1), xc(2));
Area2=integral(sf,xc(2), xc(3));
Area3=integral(sf,xc(3), xc(4));
Area4=integral(sf,xc(4), xc(5));
Star Strider
2016-12-12
Please attach your data as a ‘.mat’ file. That makes it easier for me to load it and use it.
Star Strider
2016-12-12
This works:
The Code —
D = load('friet data.mat');
x = D.x;
s = D.s;
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
xc = zci(s); % Zero-Crossing Indices Of ‘s’
ixv = [xc xc-1]; % Index Range Matrix
for k1 = 1:size(ixv,1)
xzc(k1) = interp1(s(ixv(k1,:)), x(ixv(k1,:)), 0, 'linear','extrap'); % Interpolated Zero-Crossings
end
[pky,pkix] = findpeaks( s, 'MinPeakHeight',10); % Peak & Index
[vly,vlix] = findpeaks(-s, 'MinPeakHeight',10); % Valley & Index
% xsrt = sort([xzc x(pkix), x(vlix)]);
ixsrt = sort([xc; pkix; vlix]); % Sort Indices
ixsrt = ixsrt(:)';
xsrt = x(ixsrt); % Sort ‘x’ Values
for k1 = 1:length(ixsrt)-1
ixrng = ixsrt(k1):ixsrt(k1+1);
int_s(k1) = trapz(x(ixrng), s(ixrng));
end
cm = colormap(jet(4));
figure(1)
plot(x, s)
hold on
for k1 = 1:4
ixrng = [ixsrt(k1):ixsrt(k1+1)]';
hp{k1} = patch(x([ixrng; flipud(ixrng)])', [s(ixrng); zeros(size(ixrng))]', cm(k1,:));
lgnd{k1} = sprintf('Area %d = %7.2f', k1, int_s(k1));
end
hold off
grid
legend([hp{:}], lgnd{:}, 'Location','NW')
The Plot —
I also created and attached the ‘.mat’ file of your data.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)