Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i have 2 reflection coefficient equations R1 and R2 from K with condition absolute must below 1, i use if command for this situation .But when i plot the graph the absolute reflection coefficient still above 1. (K is matrix with 1 column and 201 row)
R1=K+sqrt(K.^2-1);
R2=K-sqrt(K.^2-1);
if abs(R1)<1
r=R1;
else
r=R2;
end
this is the K in excel
real imaginer
-0.7536 0.0512
-0.802 0.0426
-0.8496 0.0408
-0.8872 0.0327
-0.927 0.0338
-0.9575 0.0242
-0.979 0.0174
-0.9977 0.0113
-10,031 0.0029
-10,012 -0.007
-0.9876 -0.0167
-0.9654 -0.0249
-0.9299 -0.0401
-0.8797 -0.0488
-0.8176 -0.0623
-0.7297 -0.0782
-0.6458 -0.0865
-0.5351 -0.1051
-0.4098 -0.1197
-0.2701 -0.1349
-0.1177 -0.1489
0.0536 -0.1699
0.213 -0.1853
0.3933 -0.1921
0.5519 -0.1857
0.7128 -0.1896
0.8511 -0.1712
0.9468 -0.1452
10,222 -0.0943
10,375 -0.04
10,134 0.0365
0.9361 0.1255
0.8122 0.2168
0.6622 0.3108
0.4657 0.3774
0.2577 0.4497
0.0431 0.4775
-0.1463 0.5093
-0.3442 0.4999
-0.5203 0.4782
-0.6692 0.4417
-0.7781 0.3822
-0.8856 0.3293
-0.9703 0.2615
-10,187 0.193
-10,524 0.1254
-10,614 0.0557
-10,539 -0.0016
-10,297 -0.0698
-0.9879 -0.1212
-0.9355 -0.1829
-0.8721 -0.2298
-0.8011 -0.2783
-0.7232 -0.325
-0.6401 -0.3586
-0.5455 -0.4008
-0.4429 -0.43
-0.3524 -0.4433
-0.2455 -0.4769
-0.1336 -0.4863
-0.0391 -0.5073
0.0779 -0.5105
0.1776 -0.5196
0.2869 -0.5152
0.3893 -0.5084
0.4831 -0.4978
0.5888 -0.4907
0.6822 -0.4574
0.7614 -0.4381
0.8484 -0.4017
0.9098 -0.3585
0.9771 -0.3172
10,268 -0.2607
10,667 -0.2102
10,969 -0.1464
11,115 -0.0724
11,141 -0.0019
10,981 0.0838
10,645 0.1546
10,135 0.2457
0.9409 0.3332
0.8657 0.4061
0.7519 0.4973
0.6426 0.5635
0.5072 0.6302
0.3633 0.6782
0.2148 0.7161
0.0382 0.7573
-0.1051 0.7395
-0.273 0.7359
-0.4273 0.7154
-0.5653 0.6794
-0.6971 0.6279
-0.8202 0.555
-0.905 0.493
-0.9996 0.4155
-10,716 0.3239
-11,006 0.2549
-11,444 0.1479
-11,464 0.0722
-11,493 -0.0031
-11,282 -0.0814
-11,040 -0.1603
-10,645 -0.2219
-10,187 -0.2787
-0.9514 -0.3223
-0.8878 -0.3841
-0.8225 -0.42
-0.7415 -0.4606
-0.6607 -0.4889
-0.5577 -0.5319
-0.482 -0.5512
-0.3775 -0.5614
-0.2918 -0.5798
-0.1621 -0.5712
-0.0979 -0.5917
0.0149 -0.5559
0.1062 -0.5734
0.2142 -0.5648
0.3159 -0.5363
0.3844 -0.5302
0.5019 -0.5066
0.5805 -0.4709
0.6626 -0.4506
0.7482 -0.4117
0.8005 -0.363
0.8799 -0.3378
0.9349 -0.2889
0.9883 -0.2449
10,306 -0.1946
10,643 -0.1373
10,870 -0.1025
10,935 -0.0389
10,840 0.0184
10,732 0.0639
10,333 0.1274
0.9906 0.1739
0.9243 0.2293
0.8455 0.2752
0.7527 0.3035
0.6292 0.3394
0.5384 0.3524
0.3808 0.3845
0.2509 0.4067
0.0931 0.4004
-0.0423 0.3839
-0.2123 0.377
-0.3666 0.3537
-0.4838 0.3309
-0.6157 0.288
-0.7211 0.2604
-0.8322 0.2172
-0.8947 0.1791
-0.9618 0.1366
-10,024 0.0932
-10,299 0.0493
-10,415 0.0099
-10,333 -0.0243
-10,092 -0.0612
-0.9798 -0.0906
-0.9321 -0.1302
-0.8796 -0.1472
-0.8121 -0.17
-0.7414 -0.1886
-0.6649 -0.2019
-0.5907 -0.2149
-0.4793 -0.2271
-0.4011 -0.2224
-0.3121 -0.2408
-0.1948 -0.2343
-0.0997 -0.2322
0.008 -0.2328
0.1304 -0.2224
0.2662 -0.2213
0.4093 -0.2298
0.553 -0.2406
0.7094 -0.3018
0.8613 -0.383
0.9745 -0.5634
0.9796 -0.8226
0.7781 -0.9412
0.6424 -0.8495
0.6264 -0.8147
0.6071 -0.6706
0.6682 -0.6029
0.6759 -0.5596
0.71 -0.5218
0.7479 -0.4825
0.7691 -0.4476
0.8264 -0.4056
0.8412 -0.3912
0.8511 -0.3813
0.8689 -0.3425
0.899 -0.3375
0.8827 -0.3198
0.9024 -0.3164
0.929 -0.2876
0.9106 -0.2855
0.9695 -0.2079
10,342 -0.5353
0.8692 -0.5046
I am not 100% sure exactly what you are asking, but I believe the problem you are experiencing is that r is above 1?
K is an imaginary number, where the first column is the real part and the second column is the imaginary part, do I have that correctly? So the first K value is -0.7536+0.0512i, right?
Ok, so did you perhaps intend to cycle through each position of the R1 matrix and see if each one was less than 1. Because right now what you are doing is saying if any values in the entire R1 vector are less than 1, then r equals to the entire R2 vector.
If you want to go through each position in the vector, you should do this:
R1=K+sqrt(K.^2-1);
R2=K-sqrt(K.^2-1);
l=length(R1);
for p=1:l
if abs(R1(p))<1
r(p)=R1(p);
else
r(p)=R2(p);
end
end
This is some code I wrote to search for the peaks of a very clean (no noise) signal where fun is an array containing evenly sampled data of a sine wave.
J=[fun(1)];
K=[1];
count=1;
for i=2:1.0:(length(fun)-2)
if fun(i-1)<fun(i) && fun(i)>fun(i+1)
J=[J,fun(i+1)];
K=[K,count+1];
end
count=count+1;
end
Included below is the data that I am trying to process.
The code found the peaks at the 664th and 991st entry, but none of the ones in between. I wrote the same algorithm in c++ and got the same result, so it is an algorithm problem, not language specific.
Please help me find the error or give me another solution.
fun = -1*pi/180*[-90.15
-90.00
-89.70
-89.10
-88.50
-87.75
-86.70
-85.65
-84.30
-82.95
-81.45
-79.80
-78.15
-76.35
-74.55
-72.30
-70.20
-67.80
-65.40
-62.70
-60.00
-57.15
-54.30
-51.15
-48.00
-44.85
-41.40
-37.95
-34.50
-30.90
-27.30
-23.55
-19.80
-16.05
-12.15
-8.25
-4.95
-1.50
1.95
4.80
7.80
10.65
13.95
17.40
20.70
23.85
27.15
30.30
33.45
36.45
39.45
42.45
45.30
48.00
50.70
53.40
55.95
58.35
60.75
63.15
65.25
67.35
69.45
71.40
73.20
74.85
76.50
78.15
79.50
80.85
82.05
83.25
84.15
85.05
85.95
86.70
87.45
88.05
88.50
88.95
89.10
89.25
89.40
89.25
89.10
88.95
88.50
88.05
87.45
86.70
86.10
85.20
84.30
83.25
82.20
81.00
79.65
78.15
76.65
75.00
73.35
71.55
69.60
67.50
65.40
63.30
60.90
58.65
56.10
53.55
51.00
48.30
45.45
42.60
39.75
36.75
33.75
30.60
27.45
24.30
21.00
17.70
14.40
11.10
7.65
4.80
1.95
-0.90
-4.35
-7.65
-11.10
-14.85
-18.75
-22.35
-26.10
-29.70
-33.30
-36.75
-40.20
-43.50
-46.80
-49.95
-52.95
-55.95
-58.65
-61.35
-63.90
-66.45
-68.85
-70.95
-73.05
-75.00
-76.80
-78.45
-80.10
-81.60
-82.95
-84.15
-85.20
-86.10
-87.00
-87.60
-88.05
-88.50
-88.80
-88.80
-88.80
-88.80
-88.50
-88.05
-87.60
-87.00
-86.25
-85.50
-84.45
-83.25
-82.05
-80.55
-79.05
-77.40
-75.60
-73.65
-71.55
-69.45
-67.20
-64.65
-62.25
-59.55
-56.70
-53.85
-50.85
-47.70
-44.55
-41.25
-37.95
-34.50
-30.90
-27.30
-23.70
-19.95
-16.20
-12.45
-8.55
-5.25
-1.95
1.50
4.35
7.20
10.05
13.35
16.65
19.95
23.10
26.40
29.55
32.55
35.55
38.55
41.40
44.25
47.10
49.80
52.35
54.90
57.30
59.70
61.95
64.05
66.30
68.25
70.20
72.00
73.65
75.30
76.80
78.30
79.65
80.85
81.90
82.95
83.85
84.75
85.50
86.10
86.55
87.00
87.45
87.60
87.75
87.75
87.75
87.60
87.30
87.00
86.55
85.95
85.35
84.60
83.70
82.80
81.75
80.55
79.35
78.00
76.50
75.00
73.35
71.70
69.75
67.95
65.85
63.75
61.50
59.25
56.85
54.45
51.90
49.35
46.65
43.80
40.95
38.10
35.10
32.10
28.95
25.95
22.65
19.50
16.20
13.05
9.75
6.90
4.05
1.05
-1.80
-5.10
-8.40
-11.70
-15.45
-19.20
-22.95
-26.55
-30.15
-33.60
-37.05
-40.35
-43.65
-46.80
-49.95
-52.80
-55.65
-58.50
-61.05
-63.60
-66.00
-68.25
-70.50
-72.45
-74.40
-76.20
-77.85
-79.35
-80.70
-81.90
-83.10
-84.15
-85.05
-85.80
-86.40
-86.85
-87.15
-87.45
-87.45
-87.45
-87.30
-87.00
-86.55
-85.95
-85.35
-84.45
-83.55
-82.50
-81.30
-79.95
-78.45
-76.95
-75.15
-73.35
-71.40
-69.30
-67.05
-64.65
-62.25
-59.70
-57.00
-54.15
-51.30
-48.30
-45.15
-41.85
-38.55
-35.25
-31.80
-28.20
-24.60
-21.00
-17.25
-13.65
-9.90
-6.60
-3.30
0.15
2.85
5.70
8.55
11.40
14.70
17.85
21.15
24.30
27.45
30.45
33.45
36.45
39.30
42.15
44.85
47.70
50.25
52.80
55.20
57.60
59.85
62.10
64.20
66.30
68.10
70.05
71.70
73.35
75.00
76.35
77.70
79.05
80.25
81.30
82.20
83.10
83.85
84.45
85.05
85.50
85.95
86.10
86.40
86.40
86.40
86.25
86.10
85.65
85.35
84.75
84.15
83.40
82.65
81.75
80.70
79.50
78.30
77.10
75.60
74.10
72.45
70.80
69.00
67.05
65.10
63.15
60.90
58.65
56.40
54.00
51.45
48.90
46.20
43.50
40.65
37.80
34.95
31.95
28.95
25.80
22.65
19.50
16.35
13.05
9.90
7.05
4.20
1.35
-1.50
-4.65
-7.95
-11.25
-15.00
-18.75
-22.35
-25.95
-29.40
-32.85
-36.30
-39.60
-42.75
-45.90
-49.05
-51.90
-54.75
-57.45
-60.15
-62.55
-64.95
-67.20
-69.30
-71.40
-73.20
-75.00
-76.65
-78.15
-79.50
-80.70
-81.90
-82.80
-83.70
-84.45
-85.05
-85.50
-85.80
-85.95
-86.10
-86.10
-85.80
-85.50
-85.05
-84.60
-83.85
-82.95
-82.05
-81.00
-79.65
-78.30
-76.95
-75.30
-73.65
-71.70
-69.75
-67.65
-65.40
-63.15
-60.60
-58.05
-55.35
-52.50
-49.65
-46.65
-43.50
-40.35
-37.05
-33.60
-30.15
-26.70
-23.10
-19.50
-15.90
-12.15
-8.55
-5.25
-1.95
1.35
4.05
6.90
9.75
12.45
15.75
18.90
22.05
25.05
28.20
31.20
34.20
37.05
39.90
42.60
45.30
48.00
50.55
53.10
55.35
57.75
60.00
62.10
64.20
66.15
67.95
69.75
71.40
73.05
74.55
75.90
77.10
78.30
79.50
80.55
81.30
82.20
82.95
83.55
84.00
84.45
84.75
84.90
85.05
85.05
84.90
84.75
84.45
84.15
83.55
83.10
82.35
81.60
80.70
79.65
78.60
77.55
76.20
74.85
73.35
71.85
70.20
68.40
66.60
64.65
62.55
60.45
58.35
55.95
53.70
51.15
48.75
46.05
43.35
40.65
37.80
34.95
32.10
29.10
25.95
22.95
19.80
16.65
13.50
10.20
7.05
4.20
1.50
-1.35
-4.50
-7.80
-11.10
-14.70
-18.30
-21.90
-25.50
-28.95
-32.40
-35.70
-39.00
-42.15
-45.30
-48.30
-51.15
-54.00
-56.70
-59.25
-61.65
-64.05
-66.30
-68.40
-70.35
-72.30
-73.95
-75.60
-77.10
-78.45
-79.65
-80.70
-81.60
-82.50
-83.10
-83.70
-84.15
-84.45
-84.60
-84.75
-84.60
-84.45
-84.15
-83.70
-83.10
-82.35
-81.45
-80.55
-79.35
-78.15
-76.80
-75.30
-73.65
-72.00
-70.05
-68.10
-66.00
-63.75
-61.35
-58.95
-56.40
-53.70
-50.85
-47.85
-44.85
-41.85
-38.70
-35.40
-32.10
-28.65
-25.05
-21.60
-18.00
-14.40
-10.80
-7.05
-3.90
-0.60
2.55
5.40
8.10
10.95
14.10
17.25
20.25
23.40
26.40
29.40
32.40
35.25
38.10
40.95
43.65
46.20
48.75
51.30
53.70
55.95
58.20
60.30
62.40
64.35
66.30
68.10
69.75
71.40
72.90
74.25
75.60
76.80
77.85
78.90
79.80
80.70
81.45
82.05
82.50
82.95
83.25
83.55
83.70
83.70
83.70
83.55
83.25
82.95
82.50
81.90
81.30
80.55
79.65
78.75
77.70
76.50
75.30
73.95
72.45
70.95
69.30
67.65
65.85
63.90
61.95
59.85
57.60
55.35
53.10
50.70
48.15
45.60
42.90
40.20
37.50
34.65
31.80
28.80
25.80
22.80
19.65
16.65
13.50
10.20
7.05
4.35
1.65
-1.20
-4.35
-7.50
-10.80
-14.40
-18.00
-21.45
-25.05
-28.50
-31.80
-35.10
-38.40
-41.55
-44.55
-47.55
-50.40
-53.25
-55.80
-58.35
-60.90
-63.15
-65.40
-67.35
-69.30
-71.25
-72.90
-74.55
-75.90
-77.25
-78.45
-79.50
-80.40
-81.30
-81.90
-82.50
-82.95
-83.25
-83.40
-83.40
-83.25
-83.10
-82.80
-82.35
-81.75
-81.00
-80.10
-79.05
-78.00
-76.65
-75.30
-73.80
-72.15
-70.50
-68.55
-66.60
-64.50
-62.25
-59.85
-57.30
-54.75
-52.05
-49.35
-46.35
-43.35
-40.35
-37.05
-33.90
-30.60
-27.15
-23.70
-20.25
-16.65
-13.05
-9.45
-6.30
-3.15
0.15
2.85
5.55
8.25
10.95
14.10
17.25
20.25
23.40
26.40
29.25
32.25
35.10
37.80
40.50
43.20
45.90
48.30
50.85
53.10
55.35
57.60
59.70
61.80
63.75
65.55
67.35
69.00
70.50
72.00
73.35
74.70
75.90
76.95
77.85
78.75
79.65
80.25
80.85
81.45
81.75
82.05
82.35
82.50
82.50
82.35
82.20
81.90
81.45
81.00
80.40
79.80
78.90
78.15
77.10
76.05
74.85
73.65
72.30
70.80
69.30
67.65
65.85
64.05
62.10
60.15
58.05
55.80
53.55
51.30
48.90
46.35
43.80
41.10
38.40
35.70
32.85
30.00
27.00
24.00
21.00
18.00
14.85
11.70
8.70
6.00
3.30
0.45
-2.25
-5.40
-8.55
-11.70
-15.30
-18.75
-22.20
-25.65
-29.10
-32.40
-35.70
-38.85
-41.85
-44.85
-47.85
-50.55
-53.25
-55.95
-58.35
-60.75
-63.00
-65.10
-67.05
-69.00
-70.80
-72.45
-73.95
-75.30
-76.50
-77.70
-78.75
-79.65
-80.40
-81.00
-81.45
-81.75
-82.05
-82.20
-82.05
-82.05
-81.75
-81.30
-80.70
-80.10];
Look at your data
First of all you should carefully look on your input data if your algorithm does not work as expected. Maybe it does what it is designed for but this is not what you expect. Some of your maxima are not clean local maxima. You have samples with exactly equal function values. I have drawn your data and magnified the first maximum to demonstrate it:
There are four values at index 165 to 169 that have identical numerical values. Your algorithm can not recognize a maximum of this shape.
Solutions
I have three suggestions for you.
Add precision to your data
Firstly: Look deeper in your data. They may have more precision if you take all significant digits. With a closer look your peaks might have real local maxima.
Don't re-invent the wheel
If you can solve it in matlab/octave you could just use an existing solution already able to deal with complicated situation as this:
[J,K]=findpeaks(fun,'DoubleSided')
This will give the expected result:
J =
-1.5603
1.5499
-1.5315
1.5263
-1.5080
1.5027
-1.4844
1.4792
-1.4608
1.4556
-1.4399
1.4347
K =
83
165
249
332
415
499
581
664
745
827
909
991
Use an improved algorithm
If you need to implement this method yourself you have to adapt your criterion for peak finding. For example you could use two single sided criteria and mark raising and falling and flat areas:
c(i)=1*(fun(i-1) < fun(i)) + -1*(fun(i+1) < fun(i))
This expression will produce in matlab/octave a 1 value for raising signal parts, 0 for flat parts and -1 for falling parts.
Now you can search this array for some conditions:
If you find a place without raise or fall after a raise and before falling signal you found a maximum. You also find a maximum if a fall follows a raise immediately.
I have two matrices that i have concatenated vertically. However, i want to insert 2 or more rows in between them with a string in those rows.. how do i go about doing that.?
Basically this is what i have;
A = 0.7363 0.8217 0.7904 0.5144 0.5341
0.3947 0.4299 0.9493 0.8843 0.0900
0.6834 0.8878 0.3276 0.5880 0.1117
0.7040 0.3912 0.6713 0.1548 0.1363
0.4423 0.7691 0.4386 0.1999 0.6787
0.0196 0.3968 0.8335 0.4070 0.4952
0.3309 0.8085 0.7689 0.7487 0.1897
0.4243 0.7551 0.1673 0.8256 0.4950
0.2703 0.3774 0.8620 0.7900 0.1476
0.1971 0.2160 0.9899 0.3185 0.0550
But i want it to be;
A = 0.7363 0.8217 0.7904 0.5144 0.5341
0.3947 0.4299 0.9493 0.8843 0.0900
0.6834 0.8878 0.3276 0.5880 0.1117
0.7040 0.3912 0.6713 0.1548 0.1363
0.4423 0.7691 0.4386 0.1999 0.6787
MESH PART
0.0196 0.3968 0.8335 0.4070 0.4952
0.3309 0.8085 0.7689 0.7487 0.1897
0.4243 0.7551 0.1673 0.8256 0.4950
0.2703 0.3774 0.8620 0.7900 0.1476
0.1971 0.2160 0.9899 0.3185 0.0550
Assuming CATIA can read the output correctly, you could simply set A as a cell variable, which can contain both numbers and strings of characters. This is achieved by using the brackets { }, as opposed to [ ] for numeric matrices. In your particular case, I would write:
A = {0.7363 0.8217 0.7904 0.5144 0.5341; ...
0.3947 0.4299 0.9493 0.8843 0.0900; ...
0.6834 0.8878 0.3276 0.5880 0.1117; ...
0.7040 0.3912 0.6713 0.1548 0.1363; ...
0.4423 0.7691 0.4386 0.1999 0.6787; ...
'MESH' 'PART' '-' '-' '-' ; ...
0.0196 0.3968 0.8335 0.4070 0.4952; ...
0.3309 0.8085 0.7689 0.7487 0.1897; ...
0.4243 0.7551 0.1673 0.8256 0.4950; ...
0.2703 0.3774 0.8620 0.7900 0.1476; ...
0.1971 0.2160 0.9899 0.3185 0.0550};
The '-'s next to MESH and PART are for consistency with the matrix (in this case, cell) size. I hope this works for you.
I would like to fit a function cos(2*pi*299.792258*t/x+p) to values given in the array vals to obtain values for t and p. This is my code:
myfit=fit(vals(:,1),vals(:,2),'cos(2*pi*299.792258*t/x+p)');
myfig=figure();
plot(vals(:,1),vals(:,2));hold all;
fplot(#(x) cos(2*pi*299.792258*myfit.t/x+myfit.p),[525,552]);
This yields the following plot where you can see that the fit is far off:
.
If I specify somewhat realistic start points, using myfit=fit(vals(:,1),vals(:,2),'cos(2*pi*299.792258*t/x+p)','StartPoint',[0,430]); I get a value of 424.4677 for t which fits much better, but still not perfectly (might be because the data is not 100% following the function):
However,
I am not sure how to deal with deviations in the data from the expected function like the peak between 535 and 540 which is clearly too narrow (they should get broader for higher values).
I need to perform this fit for many measurements, which is why it would be nice not to have to set start values
Is there a nice(r) way to find both, t and p? I thought about taking the Fourier transform, but it doesn't work with my non-uniform spacing of data points.
Here's the data for vals that I was using for my example.
525.0000 -1.1000
525.0500 -0.9531
525.1000 -0.7108
525.1500 -0.5938
525.2000 -0.4685
525.2500 -0.3344
525.3000 -0.3640
525.3500 -0.1657
525.4000 -0.0321
525.4400 0.1252
525.4900 0.1110
525.5400 0.4472
525.5900 0.4995
525.6400 0.5651
525.6900 0.8409
525.7400 1.0000
525.7900 0.8394
525.8400 0.6728
525.8800 0.5172
525.9300 0.4648
525.9800 0.3428
526.0300 0.2219
526.0800 0.0458
526.1300 -0.0933
526.1800 -0.3774
526.2300 -0.4071
526.2800 -0.5634
526.3300 -0.5759
526.3700 -0.7518
526.4200 -0.9367
526.4700 -1.0965
526.5200 -1.0953
526.5700 -0.9659
526.6200 -0.9670
526.6700 -1.0120
526.7200 -1.0000
526.7700 -0.9044
526.8200 -0.8829
526.8600 -0.8024
526.9100 -0.6515
526.9600 -0.5906
527.0100 -0.5775
527.0600 -0.5628
527.1100 -0.3677
527.1600 -0.2762
527.2100 -0.1161
527.2600 0.0335
527.3100 0.1088
527.3500 0.2409
527.4000 0.3748
527.4500 0.5507
527.5000 0.5926
527.5500 0.7832
527.6000 0.8023
527.6500 0.8028
527.7000 0.8516
527.7500 0.9808
527.7900 1.0000
527.8400 0.9331
527.8900 0.9773
527.9400 0.8937
527.9900 0.7885
528.0400 0.7090
528.0900 0.6356
528.1400 0.6109
528.1900 0.4625
528.2400 0.3755
528.2800 0.2070
528.3300 0.0542
528.3800 -0.0264
528.4300 -0.1577
528.4800 -0.4389
528.5300 -0.6134
528.5800 -0.6532
528.6300 -0.7401
528.6800 -0.8190
528.7300 -0.8971
528.7700 -0.9827
528.8200 -0.9645
528.8700 -1.0000
528.9200 -0.9400
528.9700 -0.9053
529.0200 -0.7697
529.0700 -0.5713
529.1200 -0.4241
529.1700 -0.2051
529.2100 0.0135
529.2600 0.2426
529.3100 0.3499
529.3600 0.4279
529.4100 0.5036
529.4600 0.7153
529.5100 0.7778
529.5600 0.8562
529.6100 0.8958
529.6600 0.9538
529.7000 1.0108
529.7500 1.0091
529.8000 1.0000
529.8500 0.9402
529.9000 0.8784
529.9500 0.8820
530.0000 0.8340
530.0500 0.7770
530.1000 0.6969
530.1500 0.6544
530.1900 0.5121
530.2400 0.4394
530.2900 0.3541
530.3400 0.2367
530.3900 0.1156
530.4400 0.0206
530.4900 -0.1054
530.5400 -0.2046
530.5900 -0.2951
530.6300 -0.3490
530.6800 -0.5051
530.7300 -0.6063
530.7800 -0.7500
530.8300 -0.8191
530.8800 -0.8779
530.9300 -0.8637
530.9800 -0.8580
531.0300 -0.9665
531.0800 -1.0000
531.1200 -0.9702
531.1700 -0.9975
531.2200 -0.9919
531.2700 -0.9271
531.3200 -0.8668
531.3700 -0.8524
531.4200 -0.8012
531.4700 -0.7345
531.5200 -0.5624
531.5700 -0.3037
531.6100 -0.2050
531.6600 -0.1268
531.7100 -0.1359
531.7600 0.0996
531.8100 0.3117
531.8600 0.4100
531.9100 0.5910
531.9600 0.8181
532.0100 0.8417
532.0600 0.9448
532.1000 0.9096
532.1500 1.0000
532.2000 0.9737
532.2500 0.8031
532.3000 0.5236
532.3500 0.4489
532.4000 0.4240
532.4500 0.3079
532.5000 0.1911
532.5400 -0.0560
532.5900 -0.2665
532.6400 -0.4700
532.6900 -0.5619
532.7400 -0.6287
532.7900 -0.6853
532.8400 -0.7193
532.8900 -0.8840
532.9400 -0.9882
532.9900 -0.9498
533.0300 -0.9596
533.0800 -0.9790
533.1300 -1.0000
533.1800 -0.9380
533.2300 -0.9404
533.2800 -0.8582
533.3300 -0.7732
533.3800 -0.6395
533.4300 -0.5452
533.4800 -0.4859
533.5200 -0.4455
533.5700 -0.3387
533.6200 -0.2137
533.6700 -0.0607
533.7200 0.0415
533.7700 0.1501
533.8200 0.2585
533.8700 0.3499
533.9200 0.4231
533.9600 0.5564
534.0100 0.6348
534.0600 0.7583
534.1100 0.7754
534.1600 0.8272
534.2100 0.8538
534.2600 0.9319
534.3100 0.9217
534.3600 0.9750
534.4100 0.9934
534.4500 0.9922
534.5000 0.9878
534.5500 1.0000
534.6000 0.9497
534.6500 0.9044
534.7000 0.8488
534.7500 0.8210
534.8000 0.7329
534.8500 0.7144
534.9000 0.6669
534.9400 0.5800
534.9900 0.4744
535.0400 0.3950
535.0900 0.2793
535.1400 0.1530
535.1900 0.0600
535.2400 -0.0211
535.2900 -0.1341
535.3400 -0.2141
535.3800 -0.3233
535.4300 -0.3941
535.4800 -0.5181
535.5300 -0.5882
535.5800 -0.7054
535.6300 -0.8081
535.6800 -0.8747
535.7300 -0.9486
535.7800 -1.0000
535.8300 -1.0371
535.8700 -1.0417
535.9200 -1.1081
535.9700 -1.2124
536.0200 -1.2970
536.0700 -1.2946
536.1200 -1.2352
536.1700 -1.2726
536.2200 -1.2838
536.2700 -1.2992
536.3200 -1.0946
536.3600 -1.0587
536.4100 -0.9981
536.4600 -0.6557
536.5100 -0.7914
536.5600 -0.5578
536.6100 -0.4713
536.6600 -0.1590
536.7100 0.3006
536.7600 0.8302
536.8000 1.0000
536.8500 0.9092
536.9000 0.9436
536.9500 0.8408
537.0000 1.0000
537.0500 0.4950
537.1000 0.3749
537.1500 0.2737
537.2000 -0.1488
537.2500 -0.1851
537.2900 -0.5675
537.3400 -0.7166
537.3900 -0.7574
537.4400 -0.6974
537.4900 -0.7826
537.5400 -0.8295
537.5900 -0.8402
537.6400 -0.9679
537.6900 -0.9624
537.7400 -1.0440
537.7800 -1.1150
537.8300 -1.1104
537.8800 -1.1265
537.9300 -1.0608
537.9800 -1.0299
538.0300 -1.0423
538.0800 -1.0741
538.1300 -1.0107
538.1800 -1.0658
538.2300 -1.0167
538.2700 -1.0000
538.3200 -0.9282
538.3700 -0.8739
538.4200 -0.8625
538.4700 -0.7903
538.5200 -0.7136
538.5700 -0.6588
538.6200 -0.5777
538.6700 -0.5083
538.7100 -0.4690
538.7600 -0.3299
538.8100 -0.2286
538.8600 -0.1704
538.9100 -0.0719
538.9600 0.0083
539.0100 0.1137
539.0600 0.1972
539.1100 0.3075
539.1600 0.4015
539.2000 0.4803
539.2500 0.5866
539.3000 0.6844
539.3500 0.7447
539.4000 0.7833
539.4500 0.8842
539.5000 0.9298
539.5500 0.9515
539.6000 0.9604
539.6500 0.9959
539.6900 1.0000
539.7400 0.9990
539.7900 0.9872
539.8400 0.9631
539.8900 0.9221
539.9400 0.9133
539.9900 0.8637
540.0400 0.8377
540.0900 0.8204
540.1300 0.8116
540.1800 0.7283
540.2300 0.6547
540.2800 0.6080
540.3300 0.5645
540.3800 0.5043
540.4300 0.4358
540.4800 0.3722
540.5300 0.3259
540.5800 0.2286
540.6200 0.2023
540.6700 0.0963
540.7200 -0.0249
540.7700 -0.1430
540.8200 -0.2607
540.8700 -0.3918
540.9200 -0.4483
540.9700 -0.5095
541.0200 -0.5836
541.0700 -0.6704
541.1100 -0.7295
541.1600 -0.7924
541.2100 -0.8971
541.2600 -0.9749
541.3100 -0.9550
541.3600 -0.9783
541.4100 -1.0000
541.4600 -0.9650
541.5100 -0.9674
541.5500 -0.8814
541.6000 -0.8534
541.6500 -0.8102
541.7000 -0.7576
541.7500 -0.6679
541.8000 -0.6153
541.8500 -0.5856
541.9000 -0.4698
541.9500 -0.3490
542.0000 -0.2035
542.0400 -0.1218
542.0900 -0.0102
542.1400 0.1237
542.1900 0.2250
542.2400 0.2819
542.2900 0.3594
542.3400 0.4767
542.3900 0.5554
542.4400 0.6098
542.4900 0.6575
542.5300 0.7315
542.5800 0.8207
542.6300 0.8470
542.6800 0.8921
542.7300 0.8906
542.7800 0.9144
542.8300 0.9315
542.8800 0.9628
542.9300 0.9508
542.9800 1.0000
543.0200 0.9541
543.0700 0.9344
543.1200 0.8845
543.1700 0.9157
543.2200 0.8910
543.2700 0.8914
543.3200 0.7862
543.3700 0.7562
543.4200 0.7119
543.4600 0.6365
543.5100 0.4962
543.5600 0.3867
543.6100 0.2561
543.6600 0.1928
543.7100 0.1094
543.7600 0.1054
543.8100 -0.0342
543.8600 -0.1034
543.9100 -0.2209
543.9500 -0.2948
544.0000 -0.4151
544.0500 -0.4166
544.1000 -0.5028
544.1500 -0.5590
544.2000 -0.6216
544.2500 -0.6051
544.3000 -0.6577
544.3500 -0.7203
544.4000 -0.8148
544.4400 -0.8656
544.4900 -1.0000
544.5400 -0.9584
544.5900 -0.9993
544.6400 -1.0445
544.6900 -1.0358
544.7400 -1.0597
544.7900 -1.1020
544.8400 -1.0974
544.8800 -1.1099
544.9300 -1.0965
544.9800 -1.1110
545.0300 -0.9856
545.0800 -0.9418
545.1300 -0.8764
545.1800 -0.8397
545.2300 -0.7457
545.2800 -0.7009
545.3300 -0.5820
545.3700 -0.4724
545.4200 -0.3525
545.4700 -0.1648
545.5200 -0.0132
545.5700 0.2190
545.6200 0.3475
545.6700 0.4397
545.7200 0.5649
545.7700 0.5535
545.8200 0.7693
545.8600 0.9241
545.9100 0.9968
545.9600 1.0000
546.0100 0.9528
546.0600 0.8844
546.1100 0.8604
546.1600 0.8692
546.2100 0.7248
546.2600 0.6089
546.3000 0.3552
546.3500 0.2552
546.4000 0.1719
546.4500 0.1662
546.5000 0.0449
546.5500 -0.0490
546.6000 -0.1754
546.6500 -0.2916
546.7000 -0.3144
546.7500 -0.3588
546.7900 -0.4253
546.8400 -0.5250
546.8900 -0.6858
546.9400 -0.7997
546.9900 -0.8641
547.0400 -0.8925
547.0900 -0.8946
547.1400 -0.9498
547.1900 -0.9727
547.2400 -1.0246
547.2800 -1.0507
547.3300 -1.0421
547.3800 -1.0257
547.4300 -1.0273
547.4800 -0.9969
547.5300 -1.0000
547.5800 -0.9510
547.6300 -0.9157
547.6800 -0.8474
547.7300 -0.8093
547.7700 -0.7610
547.8200 -0.7131
547.8700 -0.6131
547.9200 -0.5680
547.9700 -0.4640
548.0200 -0.4302
548.0700 -0.3732
548.1200 -0.2839
548.1700 -0.2637
548.2100 -0.2200
548.2600 -0.1664
548.3100 -0.1274
548.3600 -0.0538
548.4100 0.0337
548.4600 0.1273
548.5100 0.2211
548.5600 0.2582
548.6100 0.2802
548.6600 0.3311
548.7000 0.3429
548.7500 0.4219
548.8000 0.4644
548.8500 0.5038
548.9000 0.5790
548.9500 0.6305
549.0000 0.6990
549.0500 0.7754
549.1000 0.8110
549.1500 0.8616
549.1900 0.8614
549.2400 0.8697
549.2900 0.9070
549.3400 0.9382
549.3900 0.9429
549.4400 0.9809
549.4900 1.0000
549.5400 0.9999
549.5900 0.9758
549.6300 0.9330
549.6800 0.8869
549.7300 0.8409
549.7800 0.7811
549.8300 0.7373
549.8800 0.6869
549.9300 0.6711
549.9800 0.6422
550.0300 0.5927
550.0800 0.5134
550.1200 0.4720
550.1700 0.4076
550.2200 0.3190
550.2700 0.2680
550.3200 0.2257
550.3700 0.1799
550.4200 0.1487
550.4700 0.0835
550.5200 0.0674
550.5700 0.0218
550.6100 -0.0450
550.6600 -0.1028
550.7100 -0.1615
550.7600 -0.2260
550.8100 -0.2616
550.8600 -0.3127
550.9100 -0.3548
550.9600 -0.4267
551.0100 -0.5147
551.0500 -0.5597
551.1000 -0.6274
551.1500 -0.6562
551.2000 -0.6779
551.2500 -0.7514
551.3000 -0.7731
551.3500 -0.7989
551.4000 -0.8420
551.4500 -0.8679
551.5000 -0.9131
551.5400 -0.9229
551.5900 -0.9526
551.6400 -1.0000
551.6900 -1.0065
551.7400 -0.9542
551.7900 -0.9381
551.8400 -0.9151
551.8900 -0.9639
551.9400 -0.9017
551.9900 -0.8389
My optimization using "Lsqnonlin" is running into an error in the 18th iteration. I was wondering if I could see what is the current input point that the algorithm is using for each iteration. It may help me diagnose what's going wrong. Thanks
EDIT: First Pass at Solution
I created myoutput.m
function stop = myoutput(x,optimValues,state)
stop = false;
indicator = x;
disp(indicator)
Then added OutPut Fcn to me options
options = optimset('disp','iter-detailed','MaxFunEvals',1000,'TolFun',1e-5,'OutputFcn',#myoutput);
HW1Fparams= lsqnonlin(HW1Fobjfun4,x0,lb,ub,options)
But I am getting hideous looking results like these:
I'd appreciate it if someone can help me make it look nicer. Below the break is the rest of the original question.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Full code below. I am using some Financial Toolbox functions. The idea is to calibrate the Hull White One Factor Model to market data. It's a straightforward exercise and I must be specifying things incorrectly because it's totally tripping me up.
ValuationDate = '10-01-2014';
Settle = datenum(ValuationDate);
CurveDates = [735874;
735882;
735906;
735936;
735950;
736040;
736133;
736224;
736314;
736424;
736606;
736788;
736971;
737153;
737336;
737518;
737701;
737884;
738069;
738251;
738433;
738615;
738797;
738979;
739162;
739345;
739528;
739710;
739893;
740075;
740260;
740442;
740624;
740806;
740989;
741171;
741354;
741536;
741719;
741901;
742084;
742269;
742451;
742633;
742815;
742997;
743180;
743362;
743545;
743728;
743911;
744093;
744278;
744460;
744642;
744824;
745006;
745189;
745372;
745554;
745737;
745919;
746102;
746284;
746469;
746651;
746833;
747015;
747198;
747380;
747563;
747745;
747928;
748111;
748296;
748478;
748660;
748842;
749024;
749206;
749389;
749572;
749755;
749937;
750120;
750302;
750487];
ZeroRates = 1.0e-03*[0.0172;
0.0188;
0.0191;
0.0221;
0.0249;
0.0244;
0.0269;
0.0333;
0.0423;
0.0571;
0.0789;
0.1021;
0.1253;
0.1435;
0.1617;
0.1749;
0.1881;
0.1973;
0.2064;
0.2158;
0.2253;
0.2311;
0.2370;
0.2429;
0.2488;
0.2547;
0.2607;
0.2640;
0.2672;
0.2706;
0.2738;
0.2772;
0.2807;
0.2842;
0.2877;
0.2913;
0.2948;
0.2964;
0.2979;
0.2995;
0.3011;
0.3026;
0.3043;
0.3060;
0.3077;
0.3095;
0.3112;
0.3118;
0.3125;
0.3132;
0.3138;
0.3146;
0.3152;
0.3160;
0.3167;
0.3175;
0.3183;
0.3186;
0.3189;
0.3192;
0.3196;
0.3199;
0.3202;
0.3206;
0.3209;
0.3213;
0.3217;
0.3217;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3216;
0.3217;
0.3217;
0.3218;
0.3218;
0.3219;
0.3219;
0.3220;
0.3220;
0.3221;
0.3221];
Compounding = 2;
RateSpec = intenvset('Compounding', 2,'ValuationDate', ValuationDate,'StartDates', ValuationDate,'EndDates', CurveDates,'Rates', ZeroRates);
InstrumentMaturity = datenum('12-Sep-2044');
SwaptionBlackVol = [ 0.5940 0.5550 0.4450 0.3710 0.3400 0.3110 0.2910 0.2750 0.2630 0.2520 0.2250 0.2140 0.2080 0.2050;
0.5630 0.5470 0.4420 0.3690 0.3360 0.3090 0.2900 0.2740 0.2630 0.2520 0.2260 0.2150 0.2090 0.2060;
0.5760 0.5330 0.4400 0.3730 0.3410 0.3150 0.2970 0.2820 0.2700 0.2590 0.2330 0.2220 0.2170 0.2140;
0.5840 0.5020 0.4240 0.3730 0.3480 0.3240 0.3060 0.2920 0.2810 0.2710 0.2430 0.2300 0.2230 0.2190;
0.5630 0.4750 0.4100 0.3700 0.3450 0.3230 0.3070 0.2940 0.2830 0.2740 0.2470 0.2330 0.2260 0.2210;
0.5510 0.4520 0.3980 0.3660 0.3410 0.3220 0.3070 0.2950 0.2850 0.2760 0.2500 0.2360 0.2290 0.2240;
0.4630 0.4010 0.3660 0.3440 0.3250 0.3100 0.2990 0.2890 0.2790 0.2720 0.2470 0.2320 0.2260 0.2210;
0.4230 0.3750 0.3480 0.3290 0.3140 0.3030 0.2930 0.2840 0.2760 0.2690 0.2420 0.2300 0.2240 0.2190;
0.3700 0.3470 0.3280 0.3110 0.2960 0.2880 0.2800 0.2730 0.2680 0.2620 0.2360 0.2240 0.2190 0.2150;
0.3420 0.3250 0.3100 0.2970 0.2850 0.2770 0.2700 0.2640 0.2590 0.2540 0.2280 0.2180 0.2140 0.2110;
0.3230 0.3010 0.2900 0.2810 0.2720 0.2650 0.2590 0.2540 0.2500 0.2470 0.2230 0.2130 0.2090 0.2060;
0.3010 0.2860 0.2760 0.2670 0.2580 0.2530 0.2480 0.2450 0.2420 0.2390 0.2160 0.2060 0.2030 0.2000;
0.2850 0.2750 0.2650 0.2560 0.2480 0.2440 0.2400 0.2370 0.2350 0.2320 0.2100 0.2000 0.1970 0.1940;
0.2710 0.2600 0.2510 0.2440 0.2380 0.2340 0.2310 0.2290 0.2260 0.2240 0.2040 0.1940 0.1910 0.1890;
0.2580 0.2470 0.2400 0.2350 0.2300 0.2270 0.2240 0.2210 0.2190 0.2170 0.1980 0.1890 0.1860 0.1840;
0.2460 0.2370 0.2320 0.2270 0.2240 0.2210 0.2180 0.2150 0.2130 0.2110 0.1980 0.1840 0.1820 0.1800;
0.2040 0.1980 0.1950 0.1920 0.1900 0.1890 0.1890 0.1880 0.1880 0.1870 0.1720 0.1660 0.1640 0.1620;
0.1790 0.1750 0.1740 0.1730 0.1730 0.1710 0.1710 0.1700 0.1690 0.1690 0.1530 0.1510 0.1500 0.1480;
0.1650 0.1650 0.1660 0.1670 0.1680 0.1670 0.1670 0.1680 0.1680 0.1680 0.1550 0.1580 0.1560 0.1530;
0.1530 0.1570 0.1590 0.1620 0.1640 0.1650 0.1660 0.1670 0.1680 0.1690 0.1560 0.1650 0.1620 0.1590];
SwaptionExerciseDates = cellstr(['1M ';'2M ';'3M '; '6M ';'9M ';'1Y ';'18M';'2Y ';'3Y ';'4Y ';'5Y ';'6Y ';'7Y ';'8Y ';'9Y ';'10Y';'15Y';'20Y';'25Y';'30Y']);
SwaptionTenors = cellstr(['1Y ';
'2Y ';
'3Y ';
'4Y ';
'5Y ';
'6Y ';
'7Y ';
'8Y ';
'9Y ';
'10Y';
'15Y';
'20Y';
'25Y';
'30Y']);
testmat = zeros(length(SwaptionExerciseDates),1);
for i = 1:length(SwaptionExerciseDates)
if SwaptionExerciseDates{i}(end)=='Y'
testmat(i) = addtodate(Settle,str2double(SwaptionExerciseDates{i}(1:end-1)),'year');
elseif SwaptionExerciseDates{i}(end)=='M'
testmat(i)=addtodate(Settle,str2double(SwaptionExerciseDates{i}(1:end-1)),'month');
end
end
EurExDates= testmat;
EurExDatesFull = repmat(testmat,1,length(SwaptionTenors));
testmat2 = zeros(length(SwaptionExerciseDates),length(SwaptionTenors));
for i = 1:size(EurExDatesFull,1)
for j = 1:size(EurExDatesFull,2)
if SwaptionTenors{j}(end)=='Y'
testmat2(i,j) = addtodate(EurExDatesFull(i,j),str2double(SwaptionTenors{j}(1:end-1)),'year');
elseif SwaptionTenors{j}(end)=='M'
testmat2(i,j)= addtodate(EurExDatesFull(i,j),str2double(SwaptionTenors{j}(1:end-1)),'month');
end
end
end
EurMatFull = testmat2;
relidx = find(EurMatFull <= InstrumentMaturity);
SwaptionBlackPrices = zeros(size(SwaptionBlackVol));
SwaptionStrike = zeros(size(SwaptionBlackVol));
for iSwaption=1:length(SwaptionExerciseDates)
for iTenor=1:length(SwaptionTenors)
[~,SwaptionStrike(iSwaption,iTenor)] = swapbyzero(RateSpec,[NaN 0],Settle, EurMatFull(iSwaption,iTenor),...
'StartDate',EurExDatesFull(iSwaption,iTenor),'LegReset',[1 2],'Basis',2);
SwaptionBlackPrices(iSwaption,iTenor) = swaptionbyblk(RateSpec,'call', SwaptionStrike(iSwaption,iTenor),Settle, ...
EurExDatesFull(iSwaption,iTenor), EurMatFull(iSwaption,iTenor),SwaptionBlackVol(iSwaption,iTenor));
end
end
TimeSpec = hwtimespec(Settle,daysadd(Settle,30*(1:370),6), 12);
% B = (214:224) produces error free solutions.
B = (150:224);
HW1Fobjfun4 = #(x) SwaptionBlackPrices(relidx(B)) - ...
swaptionbyhw(hwtree(hwvolspec(ValuationDate,testmat,x(2),testmat,x(1),'spline'), RateSpec, TimeSpec), 'call',SwaptionStrike(relidx(B)),EurExDatesFull(relidx(B)), 0,EurExDatesFull(relidx(B)), EurMatFull(relidx(B)),'Basis',2, 'SwapReset',12);
options = optimset('disp','iter','MaxFunEvals',1000,'TolFun',1e-5);
x0 = [.1 .01];
lb = [0 0];
ub = [1 1];
HW1Fparams = lsqnonlin(HW1Fobjfun4,x0,lb,ub,options)
Your best bet may be to modify the source lsqnonlin.m file. This can be a somewhat in-depth process, but it gives you the maximum control over what's going on.
Open the file by typing lsqnonlin at the command prompt, highlighting it, then right-clicking and clicking on Open Selection. Before you do anything else, save a copy of the file to your default Matlab working directory (e.g. C:\Users\username\Documents\MATLAB\ for Windows 7. Matlab puts your default working directory at the top of the search path, so if you have a program that's the same name as a Matlab built-in one, then Matlab will find yours first and use it instead. I don't have that particular function myself, so I can't give you the exact code to put in there, but the solution should be simple enough for you to implement.
With your locally-saved version of the code open, note that on the first line of the program, there's a function declaration that looks something like
function [output1,output2,...]=lsqnonlin(input1,input2,...)
From the MATLAB help page, it looks like x is the first output. Presumably, it's called x in the code itself or something similar, but if not, just use the first output parameter. Now that we know the name of the variable that is being output, we can go through the code and find where it is being calculated. MATLAB will probably have this routine be a wrapper around a more fundamental numerical code. For lsqnonneg, it calls lsqncommon, which then calls either snls or levenbergMarquardt, depending on the details of the problem. Any code that is iteratively solving something will eventually end up in a while loop, since it has to perform the same calculation an unknown number of times to converge on a solution. Once you find the while loop, it's simply a matter of adding a little code to output whatever parameter(s) you'd like to look at.
Just remember that as long as you have a file of the same name in your workspace, you'll be calling that one, not the original code, so you may want to delete (or at lease move) your modified code after you've finished debugging.