Vous êtes sur la page 1sur 22

PROGRAMA:

function[C,L]=lagrange(X,Y)
w=length(X);
n=w-1;
L=zeros(w,w);
for k=1:n+1
V=1;
for j=1:n+1
if k~=j
V=conv(V,poly(X(j)))/(X(k)-X(j));
end
end
L(k,:)=V;
end
C=Y*L;
Ejemplo 1

X=-3:3

X=

-3 -2 -1 0 1 2 3

>> Y=4./(1+X.^2)

Y=

Columns 1 through 4

0.4 0.8 2 4

Columns 5 through 7

2 0.8 0.4

>> [C,L]=lagrange(X,Y)

C=

Columns 1 through 4

-0.04 5.31259064517897e-017 0.6 2.04697370165263e-016

Columns 5 through 7

-2.56 -2.92300905702092e-016 4

L=
Columns 1 through 4

0.00138888888888889 -0.00416666666666667 -0.00694444444444445


0.0208333333333333

-0.00833333333333333 0.0166666666666667 0.0833333333333333 -


0.166666666666667

0.0208333333333333 -0.0208333333333333 -0.270833333333333


0.270833333333333

-0.0277777777777778 9.25185853854297e-018 0.388888888888889


7.40148683083438e-017

0.0208333333333333 0.0208333333333333 -0.270833333333333 -


0.270833333333333

-0.00833333333333333 -0.0166666666666667 0.0833333333333333


0.166666666666667

0.00138888888888889 0.00416666666666667 -0.00694444444444444 -


0.0208333333333333

Columns 5 through 7

0.00555555555555555 -0.0166666666666667 0

-0.075 0.15 0

0.75 -0.75 0

-1.36111111111111 -7.40148683083438e-017 1

0.75 0.75 0

-0.075 -0.15 0

0.00555555555555556 0.0166666666666667 0

>> X=[-3:2:3]

X=

-3 -1 1 3

>> Y=4./(1+X.^2)

Y=
0.4 2 2 0.4

>> [C,L]=lagrange(X,Y)

C=

0 -0.2 0 2.2

L=

-0.0208333333333333 0.0625 0.0208333333333333 -0.0625

0.0625 -0.0625 -0.5625 0.5625

-0.0625 -0.0625 0.5625 0.5625

0.0208333333333333 0.0625 -0.0208333333333333 -0.0625

>> X=-3:0.01:3;

>> Y=4./(1+X.^2);

>> Y1=polyval(C,X);

>> plot(X,Y,'k',X,Y1,'r')

>> grid on

>> evaluuando f1(1.8)

s=polyval(C,1.8)

s=

1.552

X=[-3:3]

X=

-3 -2 -1 0 1 2 3

>> Y=4./(1+X.^2)

Y=

Columns 1 through 4

0.4 0.8 2 4

Columns 5 through 7
2 0.8 0.4

>> [C,L]=lagrange(X,Y)

C=

Columns 1 through 4

-0.04 5.31259064517897e-017 0.6 2.04697370165263e-016

Columns 5 through 7

-2.56 -2.92300905702092e-016 4

L=

Columns 1 through 4

0.00138888888888889 -0.00416666666666667 -0.00694444444444445


0.0208333333333333

-0.00833333333333333 0.0166666666666667 0.0833333333333333 -


0.166666666666667

0.0208333333333333 -0.0208333333333333 -0.270833333333333


0.270833333333333

-0.0277777777777778 9.25185853854297e-018 0.388888888888889


7.40148683083438e-017

0.0208333333333333 0.0208333333333333 -0.270833333333333 -


0.270833333333333

-0.00833333333333333 -0.0166666666666667 0.0833333333333333


0.166666666666667

0.00138888888888889 0.00416666666666667 -0.00694444444444444 -


0.0208333333333333

Columns 5 through 7

0.00555555555555555 -0.0166666666666667 0

-0.075 0.15 0

0.75 -0.75 0

-1.36111111111111 -7.40148683083438e-017 1
0.75 0.75 0

-0.075 -0.15 0

0.00555555555555556 0.0166666666666667 0

>> X=-3:0.01:3;

>> Y=4./(1+X.^2)

Y=

Columns 1 through 4

0.4 0.402410438526775 0.404841909234444 0.4072946471301

Columns 5 through 8

0.409768890345845 0.412264880185519 0.414782861172176


0.417323081096308

Columns 9 through 12

0.41988579106483 0.42247124555085 0.425079702444208


0.427711423102833

Columns 13 through 16

0.430366672404889 0.433045718801762 0.435748834371868


0.438476294875308

Columns 17 through 20

0.441228379809389 0.444005372465007 0.446807559983915


0.449635233416891

Columns 21 through 24

0.452488687782805 0.455368222128619 0.458274139590303


0.461206747454715

Columns 25 through 28

0.464166357222429 0.467153284671533 0.470167849922422


0.473210377503579

Columns 29 through 32
0.476281196418365 0.479380640212845 0.482509047044632
0.485666759752796

Columns 33 through 36

0.488854125928823 0.492071497988658 0.495319233245827


0.498597693985665

Columns 37 through 40

0.501907247540654 0.505248266366886 0.508621128121662


0.512026215742246

Columns 41 through 44

0.515463917525773 0.518934627210337 0.522438744057259


0.525976672934555

Columns 45 through 48

0.52954882440161 0.533155614795068 0.536797466315959


0.540474807118053

Columns 49 through 52

0.544188071397475 0.547937699483569 0.551724137931034


0.555547839613339

Columns 53 through 56

0.559409263817409 0.563308876339619 0.567247149583073


0.571224562656194

Columns 57 through 60

0.575241601472618 0.579298758852409 0.583396534624584


0.587535435730967

Columns 61 through 64

0.591715976331361 0.595938677910043 0.600204069383591


0.604512687210023

Columns 65 through 68

0.608865075499269 0.613261786124952 0.617703378837482


0.622190421378463
Columns 69 through 72

0.62672348959639 0.631303167563643 0.635930047694754


0.640604730865937

Columns 73 through 76

0.64532782653588 0.650099952867753 0.654921736852446


0.65979381443299

Columns 77 through 80

0.664716830630152 0.669691439669172 0.674718305107618


0.679798099964311

Columns 81 through 84

0.684931506849315 0.690119218094926 0.69536193588763


0.700660372400988

Columns 85 through 88

0.706015249929398 0.711427301022677 0.716897268621407


0.722425906192996

Columns 89 through 92

0.728013977868375 0.733662258579263 0.739371534195933


0.745142601665394

Columns 93 through 96

0.750976269149895 0.75687335616568 0.762834693721871


0.768861124459395

Columns 97 through 100

0.774953502789833 0.781112695034076 0.787339579560664


0.793635046923672

Columns 101 through 104

0.8 0.806435354125925 0.812942037232745 0.819520989981356

Columns 105 through 108

0.826173165895572 0.832899531494014 0.839701066420354


0.846578763571716
Columns 109 through 112

0.853533629224991 0.860566683160861 0.86767895878525


0.87487150324796

Columns 113 through 116

0.882145377558222 0.889501656696836 0.896941429724639


0.904465799886942

Columns 117 through 120

0.912075884713608 0.91977281611442 0.927557740469344


0.935431818713314

Columns 121 through 124

0.943396226415094 0.951452153849813 0.959600806064677


0.967843402937405

Columns 125 through 128

0.976181179226865 0.984615384615385 0.993147283742179


1.0017781562273

Columns 129 through 132

1.01050929668553 1.01934201472949 1.02827763496144


1.03731749695288

Columns 133 through 136

1.04646295521139 1.05571537913379 1.06507615294494


1.07454667562122

Columns 137 through 140

1.08412836079792 1.09382263665947 1.10363094581172


1.11355474513516

Columns 141 through 144

1.12359550561798 1.13375471216802 1.14403386340236


1.15443447141332

Columns 145 through 148


1.16495806150979 1.1756061719324 1.18638035354135
1.19728216947529

Columns 149 through 152

1.20831319478009 1.21947501600561 1.23076923076923


1.24219744728425

Columns 153 through 156

1.25376128385155 1.26546236831282 1.27730233746328


1.28928283642224

Columns 157 through 160

1.3014055179594 1.31367204177477 1.32608407373027


1.33864328503062

Columns 161 through 164

1.35135135135135 1.3642099519116 1.37722076848919


1.39038548437554

Columns 165 through 168

1.40370578326783 1.41718334809566 1.43081985977965


1.44461699591896

Columns 169 through 172

1.4585764294049 1.47269982695777 1.48698884758364


1.50144514094816

Columns 173 through 176

1.51607034566404 1.530866087489 1.54583397743082 1.5609756097561

Columns 177 through 180

1.57629255989912 1.5917863822675 1.60745860794085


1.62331074225884

Columns 181 through 184

1.63934426229508 1.65556061421299 1.67196121049992


1.68854742707586

Columns 185 through 188


1.70532060027285 1.72228202368138 1.73943294485998
1.75677456190434

Columns 189 through 192

1.77430801987225 1.79203440706062 1.80995475113122


1.82807001508158

Columns 193 through 196

1.84638109305761 1.86488880600494 1.88359389715577


1.90249702734839

Columns 197 through 200

1.92159877017679 1.94089960696783 1.960399921584 1.98009999504975

Columns 201 through 204

2 2.02009999494975 2.040399918384 2.06089958266783

Columns 205 through 208

2.08159866777685 2.10249671484888 2.12359311955829


2.14488712531503

Columns 209 through 212

2.16637781629116 2.18806411027843 2.20994475138122


2.23201830255008

Columns 213 through 216

2.25428313796213 2.27673743525528 2.29937916762474 2.322206095791

Columns 217 through 220

2.34521575984991 2.36840547101664 2.39177230327673


2.41531308495864

Columns 221 through 224

2.4390243902439 2.46290253063235 2.4869435463815 2.51114319794086

Columns 225 through 228

2.53549695740365 2.56 2.58464719565779 2.60943310065888

Columns 229 through 232


2.63435194942044 2.65939764643308 2.68456375838926
2.7098435065375

Columns 233 through 236

2.73522975929978 2.76071502519152 2.78629144608526


2.81195079086116

Columns 237 through 240

2.83768444948922 2.86348342758966 2.88933834151979


2.91523941403688

Columns 241 through 244

2.94117647058824 2.96713893628069 2.99311583358276


3.01909578081365

Columns 245 through 248

3.04506699147381 3.07101727447217 3.09693403530505


3.12280427824186

Columns 249 through 252

3.14861460957179 3.17435124196492 3.2 3.22554632690912

Columns 253 through 256

3.25097529258778 3.27627160291588 3.30141961043249


3.32640332640333

Columns 257 through 260

3.35120643431635 3.37581230483585 3.40020401224073


3.42436435236709

Columns 261 through 264

3.44827586206897 3.47192084020484 3.4952813701503


3.51833934382971

Columns 265 through 268

3.54107648725212 3.56347438752784 3.58551452133381


3.60717828478673

Columns 269 through 272


3.62844702467344 3.64930207097893 3.6697247706422
3.68969652246103

Columns 273 through 276

3.70919881305638 3.72821325379812 3.74672161858374


3.76470588235294

Columns 277 through 280

3.7821482602118 3.79903124703201 3.81533765738268


3.83105066564505

Columns 281 through 284

3.84615384615385 3.86063121320336 3.87446726075165


3.88764700165225

Columns 285 through 288

3.90015600624025 3.9119804400978 3.92310710082385


3.93352345363359

Columns 289 through 292

3.94321766561514 3.95217863847446 3.96039603960396


3.96786033131634

Columns 293 through 296

3.97456279809221 3.98049557169868 3.98565165404544


3.99002493765586

Columns 297 through 300

3.99361022364217 3.99640323708662 3.9984006397441 3.999600039996

Columns 301 through 304

4 3.999600039996 3.9984006397441 3.99640323708662

Columns 305 through 308

3.99361022364217 3.99002493765586 3.98565165404544


3.98049557169868

Columns 309 through 312


3.97456279809221 3.96786033131634 3.96039603960396
3.95217863847446

Columns 313 through 316

3.94321766561514 3.93352345363359 3.92310710082385


3.9119804400978

Columns 317 through 320

3.90015600624025 3.88764700165225 3.87446726075165


3.86063121320336

Columns 321 through 324

3.84615384615385 3.83105066564505 3.81533765738268


3.79903124703201

Columns 325 through 328

3.7821482602118 3.76470588235294 3.74672161858374


3.72821325379812

Columns 329 through 332

3.70919881305638 3.68969652246103 3.6697247706422


3.64930207097893

Columns 333 through 336

3.62844702467344 3.60717828478673 3.58551452133381


3.56347438752784

Columns 337 through 340

3.54107648725212 3.51833934382971 3.4952813701503


3.47192084020484

Columns 341 through 344

3.44827586206897 3.42436435236709 3.40020401224073


3.37581230483585

Columns 345 through 348

3.35120643431635 3.32640332640333 3.30141961043249


3.27627160291588
Columns 349 through 352

3.25097529258778 3.22554632690912 3.2 3.17435124196492

Columns 353 through 356

3.14861460957179 3.12280427824186 3.09693403530505


3.07101727447217

Columns 357 through 360

3.04506699147381 3.01909578081365 2.99311583358276


2.96713893628069

Columns 361 through 364

2.94117647058824 2.91523941403688 2.88933834151979


2.86348342758966

Columns 365 through 368

2.83768444948922 2.81195079086116 2.78629144608526


2.76071502519152

Columns 369 through 372

2.73522975929978 2.7098435065375 2.68456375838926


2.65939764643308

Columns 373 through 376

2.63435194942044 2.60943310065888 2.58464719565779 2.56

Columns 377 through 380

2.53549695740365 2.51114319794086 2.4869435463815


2.46290253063235

Columns 381 through 384

2.4390243902439 2.41531308495864 2.39177230327673


2.36840547101664

Columns 385 through 388

2.34521575984991 2.322206095791 2.29937916762474 2.27673743525528

Columns 389 through 392


2.25428313796213 2.23201830255008 2.20994475138122
2.18806411027843

Columns 393 through 396

2.16637781629116 2.14488712531503 2.12359311955829


2.10249671484888

Columns 397 through 400

2.08159866777685 2.06089958266783 2.040399918384 2.02009999494975

Columns 401 through 404

2 1.98009999504975 1.960399921584 1.94089960696783

Columns 405 through 408

1.92159877017679 1.90249702734839 1.88359389715577


1.86488880600494

Columns 409 through 412

1.84638109305761 1.82807001508158 1.80995475113122


1.79203440706062

Columns 413 through 416

1.77430801987225 1.75677456190434 1.73943294485998


1.72228202368138

Columns 417 through 420

1.70532060027285 1.68854742707586 1.67196121049992


1.65556061421299

Columns 421 through 424

1.63934426229508 1.62331074225884 1.60745860794085


1.5917863822675

Columns 425 through 428

1.57629255989912 1.5609756097561 1.54583397743082 1.530866087489

Columns 429 through 432

1.51607034566404 1.50144514094816 1.48698884758364


1.47269982695777
Columns 433 through 436

1.4585764294049 1.44461699591896 1.43081985977965


1.41718334809566

Columns 437 through 440

1.40370578326783 1.39038548437554 1.37722076848919


1.3642099519116

Columns 441 through 444

1.35135135135135 1.33864328503062 1.32608407373027


1.31367204177477

Columns 445 through 448

1.3014055179594 1.28928283642224 1.27730233746328


1.26546236831282

Columns 449 through 452

1.25376128385155 1.24219744728425 1.23076923076923


1.21947501600561

Columns 453 through 456

1.20831319478009 1.19728216947529 1.18638035354135


1.1756061719324

Columns 457 through 460

1.16495806150979 1.15443447141332 1.14403386340236


1.13375471216802

Columns 461 through 464

1.12359550561798 1.11355474513516 1.10363094581172


1.09382263665947

Columns 465 through 468

1.08412836079792 1.07454667562122 1.06507615294494


1.05571537913379

Columns 469 through 472


1.04646295521139 1.03731749695288 1.02827763496144
1.01934201472949

Columns 473 through 476

1.01050929668553 1.0017781562273 0.993147283742179


0.984615384615385

Columns 477 through 480

0.976181179226865 0.967843402937405 0.959600806064677


0.951452153849813

Columns 481 through 484

0.943396226415094 0.935431818713314 0.927557740469344


0.91977281611442

Columns 485 through 488

0.912075884713608 0.904465799886942 0.896941429724639


0.889501656696836

Columns 489 through 492

0.882145377558222 0.87487150324796 0.86767895878525


0.860566683160861

Columns 493 through 496

0.853533629224991 0.846578763571716 0.839701066420354


0.832899531494014

Columns 497 through 500

0.826173165895572 0.819520989981356 0.812942037232745


0.806435354125925

Columns 501 through 504

0.8 0.793635046923672 0.787339579560664 0.781112695034076

Columns 505 through 508

0.774953502789833 0.768861124459395 0.762834693721871


0.75687335616568

Columns 509 through 512


0.750976269149895 0.745142601665394 0.739371534195933
0.733662258579263

Columns 513 through 516

0.728013977868375 0.722425906192996 0.716897268621407


0.711427301022677

Columns 517 through 520

0.706015249929398 0.700660372400988 0.69536193588763


0.690119218094926

Columns 521 through 524

0.684931506849315 0.679798099964311 0.674718305107618


0.669691439669172

Columns 525 through 528

0.664716830630152 0.65979381443299 0.654921736852446


0.650099952867753

Columns 529 through 532

0.64532782653588 0.640604730865937 0.635930047694754


0.631303167563643

Columns 533 through 536

0.62672348959639 0.622190421378463 0.617703378837482


0.613261786124952

Columns 537 through 540

0.608865075499269 0.604512687210023 0.600204069383591


0.595938677910043

Columns 541 through 544

0.591715976331361 0.587535435730967 0.583396534624584


0.579298758852409

Columns 545 through 548

0.575241601472618 0.571224562656194 0.567247149583073


0.563308876339619
Columns 549 through 552

0.559409263817409 0.555547839613339 0.551724137931034


0.547937699483569

Columns 553 through 556

0.544188071397475 0.540474807118053 0.536797466315959


0.533155614795068

Columns 557 through 560

0.52954882440161 0.525976672934555 0.522438744057259


0.518934627210337

Columns 561 through 564

0.515463917525773 0.512026215742246 0.508621128121662


0.505248266366886

Columns 565 through 568

0.501907247540654 0.498597693985665 0.495319233245827


0.492071497988658

Columns 569 through 572

0.488854125928823 0.485666759752796 0.482509047044632


0.479380640212845

Columns 573 through 576

0.476281196418365 0.473210377503579 0.470167849922422


0.467153284671533

Columns 577 through 580

0.464166357222429 0.461206747454715 0.458274139590303


0.455368222128619

Columns 581 through 584

0.452488687782805 0.449635233416891 0.446807559983915


0.444005372465007

Columns 585 through 588


0.441228379809389 0.438476294875308 0.435748834371868
0.433045718801762

Columns 589 through 592

0.430366672404889 0.427711423102833 0.425079702444208


0.42247124555085

Columns 593 through 596

0.41988579106483 0.417323081096308 0.414782861172176


0.412264880185519

Columns 597 through 600

0.409768890345845 0.4072946471301 0.404841909234444


0.402410438526775

Column 601

0.4

>> Y1=polyval(C,X);

>> plot(X,Y,'k',X,Y1,'r')

>> grid on

>> s=polyval(C,1.8)

s=

0.643671040000002

>>
4

3.5

2.5

1.5

0.5

0
-3 -2 -1 0 1 2 3

6.ejemplo

X=[-2,6]

X=

-2 6

>> Y=X.^3-6*X.^2+8*X-2

Y=

-50 46

>> [C,L]=lagrange(X,Y)

C=

12 -26

L=

-0.125 0.75

0.125 0.25

>> X=-2:0.01:6;

>> Y=X.^3-6*X.^2+8*X-2;

>> Y1=polyval(C,X);

>> plot(X,Y,'k',X,Y1,'r')
>> grid on

50

40

30

20

10

-10

-20

-30

-40

-50
-2 -1 0 1 2 3 4 5 6