Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Ejemplo:
En el comando window se muestra los resultados de los cálculos realizados con las
matriz ingresada:
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA |2
end
if rem(num,i)==0
nd=nd+1;
end
i=i+1;
end
if nd==1
cp=cp+1;
disp(num);
end
num=num+1;
end
Ejemplos:
1.- Calcular: seno, coseno, tangente, cotangente, secante, cosecante de los ángulos
notables conocidos:
Código:
clc;
syms x;
y=input ('ingrese la función: ');
c=input ('ingrese la cantidad de ángulos');
i=0;
while i<c;
r=input ('ingrese el ángulo: ');
u=(r*pi) /180;
p=subs (y,x,u);
i=i+1;
disp (p);
end
t=0:360;
y1=sind(t);y2=cosd(t);y3=tand(t);y4=cotd(t);y5=secd(t);y6=cscd(t);
subplot(3,2,1);plot(t,y1);title('seno');
subplot(3,2,2);plot(t,y2);title('cos');
subplot(3,2,3);plot(t,y3);title('tan');
subplot(3,2,4);plot(t,y4);title('cot');
subplot(3,2,5);plot(t,y5);title('sec');
subplot(3,2,6);plot(t,y6);title('csc');
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA |6
Este programa pide la función y los ángulos de los cuales se quiere hallar esa función
Este programa permite resolver cualquier sistema de ecuaciones xlabel (string) Establece
la cadena string como etiqueta del eje x de la gráfica ylabel (string) Establece la cadena
Meshgrid:
La función mesh dibuja super cies 3D a partir de matrices. El comando mesh(Z) dibuja
los valores de la matriz Z sobre sus coordenadas. Los valores de Z se muestran como la
ejemplo:
[X,Y] = meshgrid(-8:.5:8);
Z = sin(R)./R;
mesh(Z);
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA |8
Z = X .* exp(-X.^2 - Y.^2);
surf(X,Y,Z)
Surf
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA |9
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
surf(x,y,z,c);
colormap([1 1 0; 0 1 1])
axis equal
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA | 10
[X,Y,Z] = peaks(30);
surfc(X,Y,Z)
colormap hsv
Shading
shading flat
shading faceted
shading interp
shading(axes_handle,...)
transformación de coordenadas
cartesianas a cilíndricas
pol 2cart(x,y);(x,y,z)
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA | 11
Otros commandos:
getframe
movie(m,n,fps)
Derivada en Matlab
Sintaxis:
diff(expr)
diff(expr,v)
diff(expr, sym('v'))
diff(expr,n)
diff(expr,v,n)
diff(expr, n, v)
Ejemplos:
1. syms x
diff(sin(x^2))
el resultado es:
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA | 12
ans = 2*x*cos(x^2)
2. syms t
diff(t^6,6)
el resultado es:
ans = 720
3. syms x t
diff(sin(x*t^2), t)
el resultado es:
ans = 2*t*x*cos(t^2*x)
Integral en Matlab
int(expr,var)
int(expr,var,Name,Value)
int(expr,var,a,b)
int(expr,var,a,b,Name,Value)
Ejemplos:
1. syms x
int(-2*x/(1 + x^2)^2)
el resultado es:
ans = 1/(x^2 + 1)
2. syms x z
int(x/(1 + z^2), z)
el resultado es:
ans = x*atan(z)
3. syms x
int(x*log(1 + x), 0, 1)
el resultado es:
ans = ¼
4. syms x t
int(2*x, sin(t), 1)
El resultado es:
ans = cos(t)^2
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA | 13
taylor(f)
taylor(f,Name,Value)
taylor(f,v)
taylor(f,v,Name,Value)
taylor(f,v,a)
taylor(f,v,a,Name,Value)
Ejemplos:
1. syms x
taylor(exp(x))
taylor(sin(x))
taylor(cos(x))
ans = x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
ans = x^5/120 - x^3/6 + x
ans = x^4/24 - x^2/2 + 1
2. syms x
taylor(log(x), x, 'Expansion Point', 1)
ans = x - (x - 1) ^2/2 + (x - 1) ^3/3 - (x - 1) ^4/4 + (x - 1) ^5/5 – 1
3. taylor(acot(x), x, 1)
ans = pi/4 - x/2 + (x - 1) ^2/4 - (x - 1) ^3/12 + (x - 1) ^5/40 + ½
4. syms x
f = sin(x)/x;
t6 = taylor(f)
t6 = x^4/120 - x^2/6 + 1
Problemas
syms x;
f=input('Ingrese la funciòn: ');
ezplot(f,[-5,5]);
xai=input('Ingrese el intervalo inferior: ');
xbi=input('Ingrese el intervalo superior: ');
tol=input('Ingrese el porcentaje de error: ');
i=1;
f1=subs(f,x,xai);
f2=subs(f,x,xbi);
ea(i)=100;
if f1*f2 < 0
xa(i)=xai; f1=subs(f,x,xa(i));
xb(i)=xbi; f2=subs(f,x,xb(i));
xr(i)=(xa(i)+xb(i))/2; f3=subs(f,x,xr(i));
fprintf('It. Xa Xr Xb
Error aprox \n');
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA | 15
%regla falsa
syms x;
f=input('Ingrese la Funcion: ');
ezplot(f,[-5,5]);
xai=input('Ingrese limite inferior: ');
xbi=input('Ingrese limite superior: ');
tol=input('Ingrese el porcentaje de Error: ');
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');
METODOS NUMERICOS PARA CIENCIAS E INGENIERIA | 16
disp('-----------------------------------------------------------
')
disp('Datos de entrada')
f = input('Función :','s');
ezplot(f,[-2,2]);
x0 = input('Aproximación inicial :');
tol= input('Tolerancia :');
disp('Datos de salida')
df = diff(sym(f));
f = inline(char(f));
df = inline(df);
x = x0;
e = abs(f(x));
i = 0;
fprintf('%s\t\t\t%s\t%s\n','it.','x','f(x)');
while (e>=tol)
fprintf('%d\t%7.11f\t%7.11f\n',i,x,f(x))
nwr = x - (f(x)/df(x));
e = abs(f(x));
x = nwr;
i = i+1;
end
Para crear un vector introducimos los valores deseados separados por espacios (o comas)
todo ello entre corchetes []. Si lo que queremos es crear una matriz lo hacemos de forma
análoga, pero separando las filas con puntos y comas (;). Generalmente usaremos letras
mayúsculas cuando nombremos a las matrices y minúsculas para vectores y escalares. Esto
ABRIR UN SCRIPT
fichero (usualmente de texto) que son ejecutadas normalmente mediante un intérprete. Son
útiles para automatizar pequeñas tareas. También puede hacer las veces de un "programa
1) Ir al menú file
Ejemplo:
Ejemplo:
Al ejecutar los dos programas anteriores, así se muestra en la ventana command window:
Ejemplo:
Problemas