GEOPHYSLAB
PROGRAMAS EN MATLAB
PROGRAMA 1: AJUSTE POR MINIMOS CUADRADOS
clear all x=0:10; y=5*x.^3+3*x.^2+12; y1=5*x.^3+3*x.^2+12+ 0.06; % plot(x,y,'o') xlabel('x' xlabel('x'); ); ylabel('f(x)' ylabel('f(x)') ) grid save('E:\archivo.txt' save('E:\archivo.txt') ) dlmwrite('E:\notas\arch1.out' dlmwrite('E:\notas\arch1.out',x, ,x,' ' ') ') dlmwrite('E:\notas\arch2.out' dlmwrite('E:\notas\arch2.out',y1, ,y1,' ' ') ') A=load('E:\notas\arch1.out' A=load('E:\notas\arch1.out'); ); B=load('E:\notas\arch2.out' B=load('E:\notas\arch2.out'); ); C=A'; D=B'; plot(C,D,'o' plot(C,D,'o'); ); pol1=polyfit(C,D,1); f1 = polyval(pol1,x); pol2=polyfit(C,D,2); f2 = polyval(pol2,x); pol3=polyfit(C,D,3); f3 = polyval(pol3,x); plot(C,D,'o' plot(C,D,'o',x,f1, ,x,f1,'.' '.',x,f2, ,x,f2,'r-' 'r-',x,f3, ,x,f3,'*' '*') )
Juvenal Tordocillo Puchuc
Página 1
GEOPHYSLAB PROGRAMA 2. METODO DE EULER
clear all %h=input('ingresar el valor del incremento'); h=0.5; n=4/h; y1(1)=0.5; x=0:h:4; for i=1:n; dy(i)=sin(x (i))-y1(i); y1(i+1)=y1(i)+h*dy(i); end y=exp(-x)+0.5*(sin(x)-cos(x)); plot(x,y,'r' plot(x,y,'r',x,y1, ,x,y1,'o' 'o'); ); % create the plot xlabel('x xlabel('x '); '); % label the x-axis ylabel(' ylabel(' function f(x)'); f(x)'); % label the y-axis title('y(x)=e(-x)+0.5*(sen(x)-cos(x)))' title('y(x)=e(-x)+0.5*(sen(x)-cos(x)))'); ); grid on
Juvenal Tordocillo Puchuc
Página 2
GEOPHYSLAB
PROGRAMA 3. METODO DE RUNGE KUTTA ORDEN
2
% programa que determina la derivada %de ecuacion diferencial por el metodo de runge kuta %de 2d0 orden clear all %h=input('ing rese el ancho de intervalos: h: '); h=0.1; t=10; y(1)=0; n=10/h; a=0.2; b=0.4; x=0:h:t; for i=2:n+1; k1=h*(a-b*y(i-1)); k2=h*(a-b*(y(i-1)+k1)); y(i)=y(i-1)+0.5*(k1+k2); end plot(x,y,'o' plot(x,y,'o') ) grid on hold on
Juvenal Tordocillo Puchuc
Página 3
GEOPHYSLAB
PROGRAMA 4. METODO DE RUNGE KUTTA ORDEN
4
% programa que determina %la ecuacion diferencial por el metodo de runge kuta %de 4to orden de un oscilador clear all h=0.1; t=50; y(1)=1; z(1)=0; n=50/h; a=0.2; b=0.4; x=0:h:t; for i=1:n; k1=h*(z(i)); l1=h*(-a*z(i)-b*y(i)); k2=h*(z(i)+l1/2); l2=h*(-a*(z(i)+l1)-b*(y(i)+k1/2)); k3=h*(z(i)+l2/2); l3=h*(-a*(z(i)+l2)-b*(y(i)+k2/2)); k4=h*(z(i)+l3/2); l4=h*(-a*(z(i)+l3)-b*(y(i)+k3/2)); y(i+1)=y(i)+((1/6)*(k1+(2*k2)+(2*k3)+k4)); z(i+1)=z(i)+((1/6)*(l1+(2*l2)+(2*l3)+l4)); end plot(x,y,'o' plot(x,y,'o',x,z, ,x,z,'r-' 'r-') ) grid on hold on
Juvenal Tordocillo Puchuc
Página 4
GEOPHYSLAB
PROGRAMA 5. METODO DE RUNGE KUTTA ORDEN
4
% programa que dete rmina la ecuación diferencial por el método de % runge kuta de 4to orden que halla la energía de un oscilador % armónico cuántico clear all h=0.01; t=10; y(1)=1; z(1)=0; n=10/h; a=3.1852; b=0.436E-9; k=0; %NIVEL DE ENERGIA SE PUEDE VARIAR x=0:h:t; for i=1:n; x(i)=i*h; k1=h*(z(i)); l1=h*(b*x(i)* x(i)-a*(k+0.5)*y(i) ); k2=h*(z(i)+l1/2); l2=h*(b*x(i)* x(i)-a*(k+(0.5))*(y (i)+0.5*k1)) ; k3=h*(z(i)+l2/2); l3=h*(-a*(z(i)+l2)-b*(y(i)+k2/2)); k4=h*(z(i)+l3/2); l4=h*(b*x(i)* x(i)-a*(k+(0.5))*(y (i)+k3)); y(i+1)=y(i)+((1/6)*(k1+(2*k2)+(2*k3)+k4)); z(i+1)=z(i)+((1/6)*(l1+(2*l2)+(2*l3)+l4)); end plot(x,y,'b-' plot(x,y,'b-',x,z, ,x,z,'r-' 'r-') ) grid on hold on
Juvenal Tordocillo Puchuc
Página 5