repaso
DESCRIPTION
36TRANSCRIPT
Ejemplo1
function [x,y]=ej1(a,b,c)
if a+b>c
disp('cobrar 1000 000');
else
disp('pagar 1000 000');
x=a+b;
y=abs(a+b-c);
end
>> [r,s]=ej1(1,0.000000,1)
pagar 1000 000
r = 1
s = 0
ejemplo2 %funcion sumatoria
function s=ej2(a,n)
s=0;
for i=1:1:n
s=s+a;
end
>>ej2(0.11,30000)
ans = 3.3000e+03
>> format long g
>> ej2(0.11,30000)
ans = 3300.00000000063
ejemplo3
%la diferencia de no utilizar funciones
a=input('ingrese a:');n=input('ingrese');
s=0;
for i=1:1:n
s=s+a;
end
disp(s)
>> ej3
ingrese a:4
ingrese 2 :
s=8
sin (ej3)(seria irracional)
ejemplo 4
%raiz enensima hallar la solucion
x^n-a=0
function x=raiz(a,n,e)
x=a;
k=0;
while abs(x^n-a)>e
x=x-(x^n-a)/(x*a^(n-1));
k=k+1;
end
end
>> raiz(7,4,0.0000001)
ans = 1.62657656741988
ejemplo 5
function [c,k]=biseccion(a,b,e)
k=0;
while b-a>=e
c=(a+b)/2;
if f(a)*f(c)>=0
a=c;
else
b=c;
end
k=k+1;
end
end
function y=f(x)
y=x+log(x);
end
>> [r,s]=biseccion(0.4,0.8,10^(-9))
r = 0.567143290489912
s = 29
ejemplo 6
function [x,k]=newton(x,e1,e2)
k=0;
y=x+1;
while (f(x))>=e1 && abs(y-x)>=e2
y=x;
x=x-f(x)/df(x);
k=k+1;
end
end
%creamos la funcion
function y=f(x)
y=(x-1)^2-8;
end
%creamos la funcion derivada
function y=df(x)
y=2*(x-1);
end
>> [x y]=newton(5,10^(-6),10^(-9))
x = 3.8284
y = 4