Iniciar sesiónCrear cuenta

# Matlab - RegulaFalsi

Vista:

## RegulaFalsi

Publicado por Vicente (3 intervenciones) el 02/05/2017 17:50:25
Esto haciendo el metodo de la Regula Falsi:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
`function [ X ] = RegulaFalsi( f,xai,xbi,tol )`
` `
`syms x;`
` `
`f1=subs(f,x,xai);`
`f2=subs(f,x,xbi);`
`i=1;`
`ea(1)=100;`
`if f1*f2 < 0`
`    xa(1)=xai;f1=subs(f,x,xa(1));`
`    xb(1)=xbi;f2=subs(f,x,xb(1));`
`    xr(1)=xa(1)-f1*(xb(1)-xa(1))/(f2-f1); f3=subs(f,x,xr(1));`
`    fprintf('It.              Xa           Xr            Xb            Error aprox \n');`
`    fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \n',i,xa(i),xr(i),xb(i));`
`    while abs(ea(i))>=tol,`
`      if f1*f3 < 0`
`         xa(i+1)=xa(i);f1=subs(f,x,xa(i+1));`
`         xb(i+1)=xr(i);f2=subs(f,x,xb(i+1));`
`      end`
`      if f1*f3> 0`
`         xa(1)=xr(i);`
`         xb(1)=xb(i);`
`      end`
`      xr(i+1)=xa(i+1)-f1*(xb(i+1)-xa(i+1))/(f2-f1);`
`      ea(i+1)=abs((xr(i+1)-xr(i))/(xr(i+1)))*100;`
`      fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.3f \n',...`
`              i+1,xa(i+1),xr(i+1),xb(i+1),ea(i+1));`
`      i=i+1;`
`   end`
`else`
`   fprintf('No existe una raíz en ese intervalo');`
`end`
`end`

1
2
3
4
5
6
7
8
9
10
11
`syms x;`
`f = x^2 -sin(x) -0.5;`
` `
```% funcion f | intervalo [0,2] | tolerancia 10^-3 | error = 10^-3 | n = 4
```
`RegulaFalsi(f,0,2,10^-3)`
` `
```% funcion f | intervalo [-1,0] | tolerancia 10^-3 | error = 10^-3 | n = 4
```
`RegulaFalsi(f,-1,0,10^-3)`
` `
```% funcion f | intervalo [-1,0] | tolerancia 10^-3 | error = 10^-3 | n = 4
```
`RegulaFalsi(f,-1,0,10^-5)`

Pero me da este error:

Index exceeds matrix dimensions.

Error in RegulaFalsi (line 24)
xr(i+1)=xa(i+1)-f1*(xb(i+1)-xa(i+1))/(f2-f1);

Error in P5_3 (line 12)
RegulaFalsi(f,0,2,10^-3)

No veo porque se sale de la matriz
Valora esta pregunta
0
Val: 6.975

## RegulaFalsi

Publicado por JOSE JEREMIAS CABALLERO (5900 intervenciones) el 02/05/2017 19:26:50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
`function  X  = RegulaFalsi( f,xai,xbi,tol )`
` syms x;`
` f1=subs(f,x,xai);`
`f2=subs(f,x,xbi);`
`i=1;`
`ea(1,1)=100;`
`if f1*f2 < 0`
`    xa(1,1)=xai;   f1=subs(f,x,xa(1,1));`
`    xb(1,1)=xbi;   f2=subs(f,x,xb(1,1));`
`    xr(1,1)=xb(1,1)-f2*(xb(1,1)-xa(1,1))/(f2-f1); f3=subs(f,x,xr(1,1));`
`    fprintf('It.           Xa           Xr            Xb            Error aprox \n');`
`    fprintf('%2d    \t %11.7f   \t %11.7f \t %11.7f \n',i,double(xa(i,1)),double(xr(i,1)),double(xb(i,1)));`
`    while abs(ea(i,1))>=tol,`
`      if f1*f3 < 0`
`         xa(i+1,1)=xa(i,1);f1=subs(f,x,xa(i+1,1));`
`         xb(i+1,1)=xr(i,1);f2=subs(f,x,xb(i+1,1));`
`      end`
`         if f1*f3> 0`
`         xa(i+1,1)=xr(i,1);`
`         xb(i+1,1)=xb(i,1);`
`         end`
`      xr(i+1,1)=xb(i+1,1)-f2*(xb(i+1,1)-xa(i+1,1))/(f2-f1);`
`       f3=subs(f,x,xr(i+1,1));`
`      ea(i+1,1)=abs((xr(i+1,1)-xr(i,1))/(xr(i+1,1)))*100;`
`      fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %7.3f \n',...`
`              i+1,double(xa(i+1,1)),double(xr(i+1,1)),double(xb(i+1,1)),double(ea(i+1,1)));`
`      i=i+1;`
` `
`   end`
`else`
`   fprintf('No existe una raíz en ese intervalo');`
`end`
`end`

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
`>> syms x; f = x^2 -sin(x) -0.5;RegulaFalsi(f,0,2,10^-3)`
`It.           Xa           Xr            Xb            Error aprox`
` 1    	   0.0000000   	   0.3235510 	   2.0000000`
` 2 	   0.3235510 	   0.5947594 	   2.0000000 	  45.600`
` 3 	   0.5947594 	   0.8220929 	   2.0000000 	  27.653`
` 4 	   0.8220929 	   1.0126495 	   2.0000000 	  18.818`
` 5 	   1.0126495 	   1.1723786 	   2.0000000 	  13.624`
` 6 	   1.1723786 	   1.3062675 	   2.0000000 	  10.250`
` 7 	   1.1723786 	   1.1942989 	   1.3062675 	   9.375`
` 8 	   1.1942989 	   1.2126305 	   1.3062675 	   1.512`
` 9 	   1.1942989 	   1.1960609 	   1.2126305 	   1.385`
`10 	   1.1960609 	   1.1976535 	   1.2126305 	   0.133`
`11 	   1.1960609 	   1.1960820 	   1.1976535 	   0.131`
`12 	   1.1960820 	   1.1961028 	   1.1976535 	   0.002`
`13 	   1.1960820 	   1.1960820 	   1.1961028 	   0.002`
`14 	   1.1960820 	   1.1960821 	   1.1961028 	   0.000`
`>> fzero('x^2-sin(x)-0.5',1)`
`ans =`
`    1.1961`

Saludos
JOSE JEREMIAS CABALLERO
Asesor de Proyectos con Matlab
Servicios de programación matlab

http://matlabcaballero.blogspot.com