clear
close all

output_precision(5)

disp("\n\
1. feladat\n\n\
A Newton iteracio keplete:\n\
x_k+1 = x_k - f(x_k)/f'(x_k)\n\n\
gyok(11) kozelitesenel az f(x) = x^2 - 11 = 0\n\
egyenletet oldjuk meg. Ekkor f'(x) = 2x, igy\n\
a Newton iteracio:\n\
x_k+1 = x_k - (x_k ^ 2 - 11)/( 2 x_k) = \n\
        x_k - x_k / 2 + 11 / (2 x_k) = \n\
        x_k / 2 + 11 / (2 * x_k)")
        
disp("\n\
Az iteracio x0 = 2-bol:")
2
for i = 1:4
  ans/2 + 11/(2*ans)
end

disp("\n\
A kozelito megoldasunk gyok(11)-re tehat: 3.3166")

disp("\n\
Ellenorzes: 3.3166^2 = ")
3.3166^2

disp("\n\
2. feladat\n\n\
")

f = @(x) (x + 1).^2 + sin(x)

disp("\n\
Szuksegunk lesz f derivaltjara:")
df = @(x) 2 * (x + 1) + cos(x)

figure (1)
x = -5 : 0.01 : 5;
plot(x, f(x))
title("2. feladat")
grid on

disp("\n\
Latszik a ket gyok es fele lejto fuggvenyszakaszok.\n\
Mivel 6 tizedesjegynyni pontossagot garantalni kell, \n\
igy noveljuk az Octave kimenetenek pontossagat:\n\
output_precision(15)")
output_precision(15)

disp("\n\
Kisebb gyok kozelitese:")
-5
for i = 1:10
  ans - f(ans) / df(ans)
end

disp("\n\
A becslesunk: -1.96156903499413\n\n\
Hogy igazoljuk, hogy 6 tizedesjeggyel helyes, meg kell\n\
neznunk, hogy elojelet valt e a fuggveny a \n\
[-1.9615689 -1.9615691] intervallumon.")

disp("\n\
f(-1.9615689) = ")
f(-1.9615689)

disp("\n\
f(-1.9615691) = ")
f(-1.9615691)

disp("\n\
Nagyobb gyok kozelitese:")
5
for i = 1:10
  ans - f(ans) / df(ans)
end

disp("\n\
A becslesunk: -0.386236870644799\n\n\
Hogy igazoljuk, hogy 6 tizedesjeggyel helyes, meg kell\n\
neznunk, hogy elojelet valt e a fuggveny a \n\
[-0.3862367 -0.3862368] intervallumon.")

disp("\n\
f(-0.3862367) = ")
f(-0.3862367)

disp("\n\
f(-0.3862369) = ")
f(-0.3862369)

disp("\n\
Valoban egyik pozitiv, masik negativ, igy a becslesunk\n\
6 tizedesjegynyileg legalabb pontos.")

disp("\n\
3. feladat\n\n\
")

disp("\n\
Legyen az x tengely a honap napjanak szama, y tengely a mert vizallas.")
x = [2 4 8 10 12]
y = [186 289 329 351 440]

disp("\n\
Elsofoku illesztesnel a 6-hoz ket legkozelebbi adatpontot hasznaljuk: 4, 8\n\n\
elsofoku = polyfit(x(2:3), y(2:3), 1)\n\n\n\
Harmadfoku illesztesnel a 6-hoz a negy legkozelebbi adatpontot hasznaljuk: 2, 4, 8, 10\n\n\
harmadfoku = polyfit(x(1:4), y(1:4), 3)\n\n\n\
Negyedfoku illesztesnel mind az 5 adatpontot hasznaljuk\n\n\
negyedfoku = polyfit(x, y, 4)")
elsofoku = polyfit(x(2:3), y(2:3), 1)
harmadfoku = polyfit(x(1:4), y(1:4), 3)
negyedfoku = polyfit(x, y, 4)

disp("\n\
A becslesek a majus 6-ai vizallasra:")

disp("\n\
polyval(elsofoku, 6) = ")
polyval(elsofoku, 6)

disp("\n\
polyval(harmadfoku, 6) = ")
polyval(harmadfoku, 6)

disp("\n\
polyval(negyedfoku, 6) = ")
polyval(negyedfoku, 6)

disp("\n\
A becslesek a majus 16-ai vizallasra:")

disp("\n\
polyval(elsofoku, 16) = ")
polyval(elsofoku, 16)

disp("\n\
polyval(harmadfoku, 16) = ")
polyval(harmadfoku, 16)

disp("\n\
polyval(negyedfoku, 16) = ")
polyval(negyedfoku, 16)

disp("\n\
Az interpolacio az interpolacios intervallumon kivul mar nem ad\n\
ertekelheto eredmenyt altalaban. Igy a 3-ad/4-ed foku polinom erteke, mely\n\
szerint Szeged viz ala kerul holnapra irrelevans.")

figure (2)
xAbrahoz = 2 : 0.01 : 16;
plot(x, y, "b+")
hold on
plot(xAbrahoz, polyval(elsofoku, xAbrahoz), "r")
plot(xAbrahoz, polyval(harmadfoku, xAbrahoz), "g")
plot(xAbrahoz, polyval(negyedfoku, xAbrahoz), "k")
title("3. feladat")
legend("meresek", "elsofoku", "harmadfoku", "negyedfoku")

disp("\n\
+. feladat\n\n\
")

% Igy generaltam a meresi adatokat:
%modell = @(x) exp(x.^2 - x);
%meresekSzama = 200;
%x = linspace(-3, 4, meresekSzama);
%zaj = randn(1, meresekSzama) * 0.3;
%y = modell(x) + zaj;

x = [ -3.00000000000000e+00, -2.96482412060302e+00, -2.92964824120603e+00, -2.89447236180905e+00 ...
  -2.85929648241206e+00, -2.82412060301508e+00, -2.78894472361809e+00, -2.75376884422111e+00 ...
  -2.71859296482412e+00, -2.68341708542714e+00, -2.64824120603015e+00, -2.61306532663317e+00 ...
  -2.57788944723618e+00, -2.54271356783920e+00, -2.50753768844221e+00, -2.47236180904523e+00 ...
  -2.43718592964824e+00, -2.40201005025126e+00, -2.36683417085427e+00, -2.33165829145729e+00 ...
  -2.29648241206030e+00, -2.26130653266332e+00, -2.22613065326633e+00, -2.19095477386935e+00 ...
  -2.15577889447236e+00, -2.12060301507538e+00, -2.08542713567839e+00, -2.05025125628141e+00 ...
  -2.01507537688442e+00, -1.97989949748744e+00, -1.94472361809045e+00, -1.90954773869347e+00 ...
  -1.87437185929648e+00, -1.83919597989950e+00, -1.80402010050251e+00, -1.76884422110553e+00 ...
  -1.73366834170854e+00, -1.69849246231156e+00, -1.66331658291457e+00, -1.62814070351759e+00 ...
  -1.59296482412060e+00, -1.55778894472362e+00, -1.52261306532663e+00, -1.48743718592965e+00 ...
  -1.45226130653266e+00, -1.41708542713568e+00, -1.38190954773869e+00, -1.34673366834171e+00 ...
  -1.31155778894472e+00, -1.27638190954774e+00, -1.24120603015075e+00, -1.20603015075377e+00 ...
  -1.17085427135678e+00, -1.13567839195980e+00, -1.10050251256281e+00, -1.06532663316583e+00 ...
  -1.03015075376884e+00, -9.94974874371859e-01, -9.59798994974874e-01, -9.24623115577889e-01 ...
  -8.89447236180905e-01, -8.54271356783920e-01, -8.19095477386935e-01, -7.83919597989950e-01 ...
  -7.48743718592965e-01, -7.13567839195980e-01, -6.78391959798995e-01, -6.43216080402010e-01 ...
  -6.08040201005025e-01, -5.72864321608040e-01, -5.37688442211055e-01, -5.02512562814070e-01 ...
  -4.67336683417086e-01, -4.32160804020100e-01, -3.96984924623116e-01, -3.61809045226131e-01 ...
  -3.26633165829146e-01, -2.91457286432161e-01, -2.56281407035176e-01, -2.21105527638191e-01 ...
  -1.85929648241206e-01, -1.50753768844221e-01, -1.15577889447236e-01, -8.04020100502512e-02 ...
  -4.52261306532664e-02, -1.00502512562812e-02,  2.51256281407035e-02,  6.03015075376883e-02 ...
   9.54773869346734e-02,  1.30653266331658e-01,  1.65829145728643e-01,  2.01005025125628e-01 ...
   2.36180904522613e-01,  2.71356783919598e-01,  3.06532663316583e-01,  3.41708542713568e-01 ...
   3.76884422110553e-01,  4.12060301507537e-01,  4.47236180904523e-01,  4.82412060301507e-01 ...
   5.17587939698493e-01,  5.52763819095477e-01,  5.87939698492462e-01,  6.23115577889447e-01 ...
   6.58291457286432e-01,  6.93467336683417e-01,  7.28643216080402e-01,  7.63819095477387e-01 ...
   7.98994974874372e-01,  8.34170854271357e-01,  8.69346733668342e-01,  9.04522613065327e-01 ...
   9.39698492462312e-01,  9.74874371859296e-01,  1.01005025125628e+00,  1.04522613065327e+00 ...
   1.08040201005025e+00,  1.11557788944724e+00,  1.15075376884422e+00,  1.18592964824121e+00 ...
   1.22110552763819e+00,  1.25628140703518e+00,  1.29145728643216e+00,  1.32663316582915e+00 ...
   1.36180904522613e+00,  1.39698492462312e+00,  1.43216080402010e+00,  1.46733668341709e+00 ...
   1.50251256281407e+00,  1.53768844221106e+00,  1.57286432160804e+00,  1.60804020100502e+00 ...
   1.64321608040201e+00,  1.67839195979900e+00,  1.71356783919598e+00,  1.74874371859296e+00 ...
   1.78391959798995e+00,  1.81909547738693e+00,  1.85427135678392e+00,  1.88944723618090e+00 ...
   1.92462311557789e+00,  1.95979899497487e+00,  1.99497487437186e+00,  2.03015075376884e+00 ...
   2.06532663316583e+00,  2.10050251256281e+00,  2.13567839195980e+00,  2.17085427135678e+00 ...
   2.20603015075377e+00,  2.24120603015075e+00,  2.27638190954774e+00,  2.31155778894472e+00 ...
   2.34673366834171e+00,  2.38190954773869e+00,  2.41708542713568e+00,  2.45226130653266e+00 ...
   2.48743718592965e+00,  2.52261306532663e+00,  2.55778894472362e+00,  2.59296482412060e+00 ...
   2.62814070351759e+00,  2.66331658291457e+00,  2.69849246231156e+00,  2.73366834170854e+00 ...
   2.76884422110553e+00,  2.80402010050251e+00,  2.83919597989950e+00,  2.87437185929648e+00 ...
   2.90954773869347e+00,  2.94472361809045e+00,  2.97989949748744e+00,  3.01507537688442e+00 ...
   3.05025125628141e+00,  3.08542713567839e+00,  3.12060301507538e+00,  3.15577889447236e+00 ...
   3.19095477386935e+00,  3.22613065326633e+00,  3.26130653266332e+00,  3.29648241206030e+00 ...
   3.33165829145729e+00,  3.36683417085427e+00,  3.40201005025126e+00,  3.43718592964824e+00 ...
   3.47236180904523e+00,  3.50753768844221e+00,  3.54271356783920e+00,  3.57788944723618e+00 ...
   3.61306532663317e+00,  3.64824120603015e+00,  3.68341708542714e+00,  3.71859296482412e+00 ...
   3.75376884422111e+00,  3.78894472361809e+00,  3.82412060301507e+00,  3.85929648241206e+00 ...
   3.89447236180905e+00,  3.92964824120603e+00,  3.96482412060301e+00,  4.00000000000000e+00 ];
   
y = [  1.62754987208471e+05,  1.27389441375901e+05,  9.99562139616594e+04,  7.86247114422172e+04 ...
   6.19983925729702e+04,  4.90095785111781e+04,  3.88381179727677e+04,  3.08534358475196e+04 ...
   2.45718773811420e+04,  1.96165351904109e+04,  1.57006257147854e+04,  1.25967129747872e+04 ...
   1.01319787345044e+04,  8.16876867285973e+03,  6.60382301563319e+03,  5.35050348628183e+03 ...
   4.34579533566640e+03,  3.53913051402191e+03,  2.88945500778068e+03,  2.36383315770213e+03 ...
   1.93979894576020e+03,  1.59514757904576e+03,  1.31470655664949e+03,  1.08692097295108e+03 ...
   9.00743608914168e+02,  7.48414129388719e+02,  6.23048534564520e+02,  5.19982594571577e+02 ...
   4.35275842366852e+02,  3.64992297070302e+02,  3.06772898019722e+02,  2.58851770150387e+02 ...
   2.18447041819427e+02,  1.85720665882761e+02,  1.56503528724682e+02,  1.33628633295403e+02 ...
   1.14744191401415e+02,  9.81673088009540e+01,  8.34340219614501e+01,  7.20367075167757e+01 ...
   6.25165377635188e+01,  5.40518846593175e+01,  4.67716707939470e+01,  4.05958594380405e+01 ...
   3.55770475183861e+01,  3.09353426567134e+01,  2.68792778629542e+01,  2.30307202415782e+01 ...
   2.08201494204622e+01,  1.77465547739784e+01,  1.60617009392797e+01,  1.37754104169811e+01 ...
   1.23958462087785e+01,  1.10659193957085e+01,  1.05199831036830e+01,  9.26227508323600e+00 ...
   7.71294650718010e+00,  7.16648334114459e+00,  6.79970090340028e+00,  5.58772690702983e+00 ...
   5.48322874213749e+00,  5.19270091685388e+00,  4.50884550494493e+00,  4.21928874195292e+00 ...
   3.71990700650812e+00,  3.61618262536443e+00,  3.32390286225629e+00,  2.67786424557076e+00 ...
   2.93874059830126e+00,  2.84609257843851e+00,  2.00963636918476e+00,  1.93685054990841e+00 ...
   2.34308076263104e+00,  1.56162173344056e+00,  2.20208498822148e+00,  2.22620993065695e+00 ...
   1.97875698167661e+00,  1.80207070867153e+00,  1.34065861527937e+00,  1.24968100972760e+00 ...
   1.65916118165769e+00,  1.32266806706021e+00,  1.31209796234944e+00,  1.22370611774579e+00 ...
   6.94977244913147e-01,  1.08577428714340e+00,  6.57783314170699e-01,  9.71828621317711e-01 ...
   6.08224835398938e-01,  7.41252185559830e-01,  7.64287580625122e-01,  5.92291711747438e-01 ...
   8.87482255648184e-01,  9.97365352229011e-01,  5.59512830394989e-01,  9.37964689751857e-01 ...
   1.17090727551570e+00,  6.71872990115376e-01,  7.79201217232950e-01,  1.09775875570427e+00 ...
   8.04142108325223e-01,  5.19097079014013e-01,  6.17517350746224e-01,  6.08942336458027e-01 ...
   8.47189343331569e-01,  8.48419101482031e-01,  1.06178036690496e+00,  5.23795111388049e-01 ...
   7.49183244771645e-01,  5.55098373293749e-01,  1.27485389514535e+00,  1.18249652561857e+00 ...
   1.02910698569455e+00,  1.11431251142877e+00,  9.41435789017567e-01,  1.24807738324271e+00 ...
   1.47760396534893e+00,  1.00196827462214e+00,  1.51911750441088e+00,  1.78445668874020e+00 ...
   1.44683319214890e+00,  1.28168133973538e+00,  1.34334316095855e+00,  1.68506792729170e+00 ...
   1.72024318670129e+00,  2.01566092070925e+00,  1.75317683341142e+00,  2.32414688355944e+00 ...
   2.04279078544101e+00,  2.60983703102352e+00,  2.81179230431093e+00,  2.83860238700073e+00 ...
   3.21791304602868e+00,  2.79871926437272e+00,  3.60258825652131e+00,  4.28055323550037e+00 ...
   3.36906504498714e+00,  4.05346443161077e+00,  4.91959516535205e+00,  5.90452453365564e+00 ...
   5.80230742334879e+00,  6.08269863378890e+00,  7.97104124162953e+00,  7.83336889064757e+00 ...
   8.90878374736232e+00,  9.99569141240186e+00,  1.11580045311137e+01,  1.20404179859155e+01 ...
   1.41395613996762e+01,  1.62309188947320e+01,  1.79854005794738e+01,  2.11396304985637e+01 ...
   2.37705890265626e+01,  2.67359067102650e+01,  3.04046790956120e+01,  3.55071964469624e+01 ...
   4.08541773134561e+01,  4.70042962460393e+01,  5.36313648859354e+01,  6.19156098156702e+01 ...
   7.22569425798671e+01,  8.37317940428253e+01,  9.82249949865469e+01,  1.14912111901562e+02 ...
   1.34145409050319e+02,  1.57966388668876e+02,  1.84808673850004e+02,  2.18602420502499e+02 ...
   2.58559110854473e+02,  3.06951689969410e+02,  3.64628251533962e+02,  4.35096039845536e+02 ...
   5.19586647348464e+02,  6.22876618346946e+02,  7.48641409741979e+02,  9.00748848225028e+02 ...
   1.08683767123012e+03,  1.31489319467088e+03,  1.59518799662437e+03,  1.94027361643712e+03 ...
   2.36394402949733e+03,  2.88883315416335e+03,  3.53893339647133e+03,  4.34606837429025e+03 ...
   5.34995165642790e+03,  6.60312562580792e+03,  8.16838046473527e+03,  1.01315993617790e+04 ...
   1.25964753523061e+04,  1.56999665583468e+04,  1.96169394205779e+04,  2.45711299872415e+04 ...
   3.08537609125591e+04,  3.88384534304882e+04,  4.90101938500283e+04,  6.19990119980843e+04 ...
   7.86247493520774e+04,  9.99556572959923e+04,  1.27389641381282e+05,  1.62754857937398e+05 ];

figure (3)
plot(x, y, "b+")
title("+ feladat: meres")
legend("meresi adatok: (x, y)")

disp("\n\
Exponencialisnak latszik a tendencia, vegyuk a logaritmust.")

figure (4)
plot(x, log(y), "b+")
title("+ feladat: meres")
legend("meresi adatok: (x, log(y))")

disp("\n\
Parabolanak latszik a kitevo, masodfoku polinomot illesztunk.")
mo = polyfit(x, log(y), 2)

figure (5)
plot(x, log(y), "b+")
hold on 
plot(x, polyval(mo, x), "r")
title("+ feladat: meres + illesztes")
legend("meresi adatok: (x, log(y))", "illesztett nodell")

figure (6)
plot(x, y, "b+")
hold on
plot(x, exp(polyval(mo, x)), "r")
title("+ feladat: meres + illesztes")
legend("meresi adatok: (x, y)", "illesztett nodell")