# Gyakorlat 2019. 05. 08. - Kidolgozando feladatok
clear
close all
output_precision(7)

disp("Gyakorlat 2019. 05. 08. - Kidolgozando feladatok");

disp("\n\
5. feladat\n\n\
Toltsuk le a feladat5adat.txt file-t. Abrazoljuk az abban levo x, y vektort!\n\
Milyen osszefuggest sejtunk kozottuk az abra alapjan?\n\
Ha megvan a megsejtett modell, keressuk meg milyen parameterek mellett\n\
illeszkedik a legjobban a meresi pontjainkhoz!")

#########################################

# csak egy az egyben be kellett masolni az adatokat a file-bol
x = [ -1.0000000, -0.9798995, -0.9597990, -0.9396985, -0.9195980, -0.8994975, -0.8793970 ...
  -0.8592965, -0.8391960, -0.8190955, -0.7989950, -0.7788945, -0.7587940, -0.7386935 ...
  -0.7185930, -0.6984925, -0.6783920, -0.6582915, -0.6381910, -0.6180905, -0.5979899 ...
  -0.5778894, -0.5577889, -0.5376884, -0.5175879, -0.4974874, -0.4773869, -0.4572864 ...
  -0.4371859, -0.4170854, -0.3969849, -0.3768844, -0.3567839, -0.3366834, -0.3165829 ...
  -0.2964824, -0.2763819, -0.2562814, -0.2361809, -0.2160804, -0.1959799, -0.1758794 ...
  -0.1557789, -0.1356784, -0.1155779, -0.0954774, -0.0753769, -0.0552764, -0.0351759 ...
  -0.0150754,  0.0050251,  0.0251256,  0.0452261,  0.0653266,  0.0854271,  0.1055276 ...
   0.1256281,  0.1457286,  0.1658291,  0.1859296,  0.2060302,  0.2261307,  0.2462312 ...
   0.2663317,  0.2864322,  0.3065327,  0.3266332,  0.3467337,  0.3668342,  0.3869347 ...
   0.4070352,  0.4271357,  0.4472362,  0.4673367,  0.4874372,  0.5075377,  0.5276382 ...
   0.5477387,  0.5678392,  0.5879397,  0.6080402,  0.6281407,  0.6482412,  0.6683417 ...
   0.6884422,  0.7085427,  0.7286432,  0.7487437,  0.7688442,  0.7889447,  0.8090452 ...
   0.8291457,  0.8492462,  0.8693467,  0.8894472,  0.9095477,  0.9296482,  0.9497487 ...
   0.9698492,  0.9899497,  1.0100503,  1.0301508,  1.0502513,  1.0703518,  1.0904523 ...
   1.1105528,  1.1306533,  1.1507538,  1.1708543,  1.1909548,  1.2110553,  1.2311558 ...
   1.2512563,  1.2713568,  1.2914573,  1.3115578,  1.3316583,  1.3517588,  1.3718593 ...
   1.3919598,  1.4120603,  1.4321608,  1.4522613,  1.4723618,  1.4924623,  1.5125628 ...
   1.5326633,  1.5527638,  1.5728643,  1.5929648,  1.6130653,  1.6331658,  1.6532663 ...
   1.6733668,  1.6934673,  1.7135678,  1.7336683,  1.7537688,  1.7738693,  1.7939698 ...
   1.8140704,  1.8341709,  1.8542714,  1.8743719,  1.8944724,  1.9145729,  1.9346734 ...
   1.9547739,  1.9748744,  1.9949749,  2.0150754,  2.0351759,  2.0552764,  2.0753769 ...
   2.0954774,  2.1155779,  2.1356784,  2.1557789,  2.1758794,  2.1959799,  2.2160804 ...
   2.2361809,  2.2562814,  2.2763819,  2.2964824,  2.3165829,  2.3366834,  2.3567839 ...
   2.3768844,  2.3969849,  2.4170854,  2.4371859,  2.4572864,  2.4773869,  2.4974874 ...
   2.5175879,  2.5376884,  2.5577889,  2.5778894,  2.5979899,  2.6180905,  2.6381910 ...
   2.6582915,  2.6783920,  2.6984925,  2.7185930,  2.7386935,  2.7587940,  2.7788945 ...
   2.7989950,  2.8190955,  2.8391960,  2.8592965,  2.8793970,  2.8994975,  2.9195980 ...
   2.9396985,  2.9597990,  2.9798995,  3.0000000 ];
   
y = [    -0.3650775,    -1.6194680,     0.7739034,     0.4389166,     0.0034334,    -0.3303152 ...
     -0.1479603,     1.2399204,     0.1126999,     1.4789613,    -0.5658299,     1.0088258 ...
     -1.9204823,     1.0668510,    -1.7639621,    -0.0920121,    -1.0432664,    -1.0309645 ...
      1.5460020,     1.7943784,    -0.3357183,    -0.7896188,    -1.6084506,    -0.0633998 ...
      1.4944166,    -0.6125945,    -0.3312900,     0.2239657,     0.7301238,     0.5000979 ...
      0.0037879,     0.6859838,     0.0399837,     0.7892236,     0.6698545,    -1.5892481 ...
      0.3570261,     0.2078351,    -1.9040644,    -0.7799117,     1.4688081,     0.8231654 ...
      0.7665000,     0.2203810,     0.5741284,     0.7983665,     1.5254742,    -3.3689961 ...
      0.6668500,    -0.5210944,    -0.0831005,     0.1509256,     0.0466312,    -1.0247179 ...
     -0.7670549,     1.8954994,    -0.2069411,     0.3373023,     1.0447940,     0.2196320 ...
      0.5764290,     0.6581982,     2.3192087,     1.6880671,     1.7004276,     0.0592860 ...
      1.2750486,    -0.6861327,     1.4256310,     1.2863793,    -0.4178094,     2.5424076 ...
      3.3330996,     0.6075441,     1.9905227,     1.9642885,     3.7318015,    -0.3741397 ...
      2.2538334,     1.7763331,     1.7884801,     1.7496926,     2.5095207,     1.7856571 ...
      3.0397648,     3.1421190,     4.1370846,     4.4366563,     5.1382189,     5.2031488 ...
      6.0513528,     6.1768739,     4.3972306,     7.2253768,     6.4095420,     7.1196466 ...
      7.3142474,     7.7453996,     8.9697311,     9.0168965,     9.5362828,    10.7156557 ...
      9.0365204,    11.1270346,    10.9894369,    12.2996801,    12.5646450,    13.0503198 ...
     13.6740808,    15.4066190,    15.8319329,    19.2602735,    17.7809603,    21.3596657 ...
     21.7554947,    24.3223825,    24.5081727,    25.4685030,    28.5889559,    29.7139554 ...
     31.0248331,    35.7385775,    34.4077372,    36.8768537,    39.6943434,    45.3885856 ...
     46.1773862,    49.2351080,    51.7825206,    55.2626890,    56.7762784,    63.1770463 ...
     65.6068078,    72.7000843,    75.4401594,    80.0502503,    85.4514253,    91.4782831 ...
     97.0310333,   104.0533759,   109.3202649,   118.5066900,   125.2444014,   133.8552327 ...
    139.4129143,   149.5962386,   158.1607025,   169.7007814,   181.2011114,   195.3907914 ...
    206.6054149,   220.6272074,   235.6976110,   249.4536558,   266.8624985,   283.2293371 ...
    301.5094219,   320.8830592,   343.2431610,   364.5245121,   389.3603273,   414.4267757 ...
    440.2601138,   467.1681155,   498.5858233,   533.5115870,   565.7652716,   603.9057296 ...
    643.3965859,   685.4288484,   730.2744051,   777.8435004,   829.0450274,   882.0311740 ...
    940.5643106,  1000.8177693,  1067.7682086,  1135.9511883,  1209.3091173,  1288.8284051 ...
   1374.2183062,  1463.1593279,  1558.6168801,  1660.6592193,  1768.2317894,  1881.4396695 ...
   2006.4833355,  2135.4150184,  2277.2168361,  2422.7417493,  2581.8328859,  2751.2630446 ...
   2928.3133866,  3119.8107492,  3324.3013846,  3540.0754114,  3773.5331284,  4019.2850536 ...
   4279.2315799,  4560.2903596 ];

figure (1)
plot(x, y, "b+")
title("5) feladat: mert adatok")
legend("(x, y) meresi adatok")

disp("\n\
Exponencialis fuggvenyhez hasonlit, terjunk at logaritmikus koordinatakra!")

figure (2)
plot(x, log(y), "b+")
title("5) feladat: mert adatok logaritmikus koordinata-rendszerben")
legend("(x, y) meresi adatok")

disp("Ez mar egyenesnek tunik (valamelyest), illeszunk ra elsofoku polinomot!")
egyutthatok = polyfit(x, log(y), 1)

figure (3)
plot(x, log(y), "b+")
hold on 
plot(x, polyval(egyutthatok, x), "r")
title("5) feladat: mert adatok logaritmikus koordinata-rendszerben + illesztett egyenes")
legend("(x, y) meresi adatok", "illesztett egyenes")

figure (4)
plot(x, y, "b+")
hold on 
plot(x, exp(polyval(egyutthatok, x)), "r")
title("5) feladat: mert adatok + modell")
legend("(x, y) meresi adatok", "illesztett modell")

disp("\n\
Egyertelmuen latszik a problema, nem talaltuk meg a jo egyenest.\n\
Miert, hiszen elvegeztuk a legkisebb negyzetes illesztest?\n\n\
Valasz: \n\
3. abran latszik jol, hogy a zaj nagyon nagy a mert ertekekben, \n\
a valodi jel csak x > 1 korultol valik dominanssa.\n\n\
Megoldas: \n\
Csak az 1-nel nagyobb x ertekeket hasznaljuk az illesztesnel!\n\n\
x vektor mar sorba van rendezve, a 100. elem 0.9899, ez mar jo lesz.\n\
200 db (x, y) meresi pontunk van, hasznaljuk ezekbol akkor a 100 - 200 kozotti\n\
indexueket!")
egyutthatok = polyfit(x(100:200), log(y(100:200)), 1)

figure (5)
plot(x, log(y), "b+")
hold on 
plot(x, polyval(egyutthatok, x), "r")
title("5) feladat: mert adatok logaritmikus koordinata-rendszerben + illesztett egyenes")
legend("(x, y) meresi adatok", "illesztett egyenes")

figure (6)
plot(x, y, "b+")
hold on 
plot(x, exp(polyval(egyutthatok, x)), "r")
title("5) feladat: mert adatok + modell")
legend("(x, y) meresi adatok", "illesztett modell")

disp("Nagyon szep az utolso abra, de amugy nehogy azt higyjuk, hogy a zaj eltunt.\n\
Nagyitsunk ra es latszik, hoy bizony ez kozelites, csak a skalazas miatt nem latjuk.")

figure (7)
abrazolandoPontok = 30;
plot(x(1:abrazolandoPontok), y(1:abrazolandoPontok), "b+")
xAbrazolas = linspace(x(1), x(abrazolandoPontok), 50); # hogy szep sima legyen az exponencialis gorbe
hold on 
plot(xAbrazolas, exp(polyval(egyutthatok, xAbrazolas)), "r")
title("5) feladat: elso 30 mert adat + modell")
legend("(x, y) meresi adatok", "illesztett modell")

figure (8)
abrazolandoPontok = 120;
plot(x(1:abrazolandoPontok), y(1:abrazolandoPontok), "b+")
xAbrazolas = linspace(x(1), x(abrazolandoPontok), 50); # hogy szep sima legyen az exponencialis gorbe
hold on 
plot(xAbrazolas, exp(polyval(egyutthatok, xAbrazolas)), "r")
title("5) feladat: elso 120 mert adat + modell")
legend("(x, y) meresi adatok", "illesztett modell")