Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Codigos para el programa de matlab, Apuntes de Métodos Numéricos

Códigos para matlab de funciones que se utiliza en la materia de metodos numericos

Tipo: Apuntes

2018/2019

Subido el 18/02/2019

Diana_Castro
Diana_Castro 🇧🇴

5

(1)

2 documentos

1 / 16

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
function taylor
f1=input('Ingrese la funcion f(x)= ','s')
X1=input('Ingrese el valor X1= ')
n=input('Ingrese el grado del polinomio de Taylor: ')
f=inline(f1)
p=f(X1)
syms x
for i=1:1:n
dfi=diff(f1,x,i)
df=inline(dfi)
p=p+(df(X1)/factorial(i))*(x-X1)^i
end
disp('El polinomio de Taylor de grado n es: ')
disp(expand(p))
end
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Vista previa parcial del texto

¡Descarga Codigos para el programa de matlab y más Apuntes en PDF de Métodos Numéricos solo en Docsity!

function taylor f1=input('Ingrese la funcion f(x)= ','s') X1=input('Ingrese el valor X1= ')

n=input('Ingrese el grado del polinomio de Taylor: ')

f=inline(f1)

p=f(X1)

syms x

for i=1:1:n dfi=diff(f1,x,i) df=inline(dfi) p=p+(df(X1)/factorial(i))*(x-X1)^i end disp('El polinomio de Taylor de grado n es: ') disp(expand(p)) end

function maxmin f1=input('ingrese la función f(x)= ', 's') a=input('ingrese el lim inferior del intervalo a= ') b=input('ingrese el lim superior del intervalo b= ') f=inline(f1) syms x df=diff(f1,x) X=solve(df) d2f=diff(f1,x,2) f2=inline(d2f) n=length(X); for i=1:1:n if f2(X(i))> disp('El punto:') disp(vpa([X(i),f(X(i))],4)) disp('Es MINIMO LOCAL') disp(' ') end if f2(X(i))< disp('El punto:') disp(vpa([X(i),f(X(i))],4)) disp(' Es MAXIMO LOCAL') disp(' ')

end if f2(X(i))== disp('El punto:') disp(vpa([X(i),f(X(i))],4)) disp(' Podria ser maximo, min o ninguno de los dos, El criterio no es concluyente.') end end x1=a:0.1:b; y1=subs(f1,x,x1); plot(x1,y1,'r'), grid

function maxmin2var

syms x y f1=input('ingrese la funcion f(x)= ','s') a=input('ingrese el limite inferior del integral es a= ') b=input('ingrese limite superior del integral b= ') n=input('ingrese numero particiones en el intervalo n= ') f=inline(f1) h=(b-a)/n I= X=zeros(1,n+1); for i=1:1:n+ X(i)=a+(i-1)h; end d2x=diff(f1,x,2) E=0;%error de truncamiento for i=1:1:n I=I+h(f(X(i))+f(X(i+1)))/2;%trapecio z=X(i):(X(i+1)-X(i))/10:X(i+1);%valores para gamma de x entre Xi y Xi+ y2=subs(d2x,x,z) K=max(abs(y2)) E=E+abs((K/12)*(h)^3); end disp('el resultado de la integral es I=') disp(vpa(I,10)) disp('el error de truncamiento es E=') disp(vpa(E,10))

function simpson syms x y f1=input('ingrese la funcion f(x)= ','s')

a=input('ingrese el limite inferior del integral a= ') b=input('ingrese limite superior del integral b= ') n=input('ingrese numero particiones n= ') f=inline(f1) h=(b-a)/(2n) H=(b-a)/n I= X=zeros(1,n+1) for i=1:1:n+ X(i)=a+(i-1)H; end d4x=diff(f1,x,4) E=0;%error de truncamiento for i=1:1:n I=I+(h/3)(f(X(i))+4f((X(i)+X(i+1))/2)+f(X(i+1))) z=X(i):(X(i+1)-X(i))/10:X(i+1);%valores para gamma de x entre Xi y Xi+ y2=subs(d4x,x,z) K=max(abs(y2)) E=E+abs((-K/90)*h.^5); end disp('el resultado de la integral es:') disp(vpa(I,10)) disp('el error de truncamiento es:') disp(vpa(E,10))

function simpson syms x y f1=input('Ingrese la funcion f(x)= ','s'); a=input('Ingrese el limite inferior de a= ');

n=input('ingrese numero particiones n= ') b=input('ingrese el valor a evaluar b = ') h=(b-x1)/n; X=zeros(1,n+1); X(1)=x1; for i=2:1:n+ X(i)=x1+(i-1)h; end Y=zeros(1,n+1); Y(1)=y1; for i=1:1:n Y(i+1)=Y(i)+subs(g,{x,y},{X(i),Y(i)})h; end plot(X,Y) disp('Los valores de X_i son: ') disp(vpa(X,10)) disp('Los valores de Y_i son: ') disp(vpa(Y,10))

function Heun g=input('Ingrese la funcion f(x,y)= ','s'); x1=input('Ingrese el valor inicial de x1= '); y1=input('Ingrese el valor inicial y1= '); b=input('Ingrese el valor a evaluar y(b), b= '); n=input('Ingrese el numero de iteraciones n= ');

syms x y h=(b-x1)/n; X=zeros(1, n+1); X(1)= x1; Y=zeros(1, n+1); YE=zeros(1, n+1); YH=zeros(1, n+1); YE(1)=y1; YH(1)=y1; for i=2:n+ X(i)=X(1)+(i-1)h; end for i=1:1:n m1=subs(g,{x,y},{X(i), YH(i)}); YE(i+1) = YH(i) + m1h; m2=subs(g,{x,y},{X(i+1), YE(i+1)}); YH(i+1)= YH(i) + ((m1+m2)/2)*h; end plot(X,YH) disp('Los valores de X(i) son: ') disp(vpa(X, 10)) disp('Los valores aproximados de Y(i) son: ') disp(YH)

function runge syms x y g=input('ingrese la funcion g(x,y)= ','s') x1=input('ingrese valor inicial eje x; x1= ') y1=input('ingrese valor inicial eje y: y1= ') b=input('ingrese el valor a determinar f(b); b= ')

sx2=0; sxy=0; for i=1:1:n sx=sx+X(i); sy=sy+Y(i); sx2=sx2+X(i)^2; sxy=sxy+X(i)Y(i); end syms a b e1=na+bsx-sy e2=asx+bsx2-sxy R=solve(e1,e2) R1=[R.a,R.b] disp('los coeficientes a y b del modelo y=a+bx respectivamente son: ') disp(vpa(R1,4)) syms x y y=R1(1,2)x+R1(1,1); ve=subs(y,x,c) disp('El valor estimado y(c) es: ') disp(vpa(ve,4)) plot(X,Y,'o') hold on ezplot(y,[min(X)-1,max(X)+1]),grid

function cuadratica X=input('ingrese los valores de xi en el vector X= ') Y=input('ingrese los valores de yi en el valor Y= ') d=input('Ingrese el valor d para estimar y(d), d= ') n=length(X); sx=0; sy=0;

sx2=0; sx3=0; sx4=0; sxy=0; sx2y=0; for i=1:1:n sx=sx+X(i); sy=sy+Y(i); sx2=sx2+X(i)^2; sx3=sx3+X(i)^3; sx4=sx4+X(i)^4; sxy=sxy+X(i)Y(i); sx2y=sx2y+(X(i)^2)Y(i);

end syms a b c e1=na+bsx+csx2-sy e2=asx+bsx2+csx3-sxy e3=asx2+bsx3+csx4-sx2y R1=solve(e1,e2,e3) R=[R1.a,R1.b,R1.c] disp('los coeficientes a y b del modelo y=a+bx+cx^ respectivamente son: ') disp(vpa(R,4)) syms x y y=R(1,1)+R(1,2)x+R(1,3)*x^2; ve=subs(y,x,d) disp('El valor estimado y(d) es: ') disp(vpa(ve,4)) plot(X,Y,'o') hold on ezplot(y,[min(X)-1,max(X)+1]), axis([min(X)-1,max(X) +1,min(Y)-1,max(Y)+1]), grid

function rexponencial X=input('ingrese los valores de xi en el vector X= ') Y=input('ingrese los valores de yi en el valor Y= ') c=input('Ingrese el valor c para estimar y(c), c= ') n=length(X); sx=0;

'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Programa1_OpeningFcn, ... 'gui_OutputFcn', @Programa1_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end

if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT

% --- Executes just before Programa1 is made visible.

function Programa1_OpeningFcn(hObject, eventdata, handles, varargin) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% handles.output = hObject;

% Update handles structure guidata(hObject, handles);

% UIWAIT makes Programa1 wait for user response (see UIRESUME) % uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line. function varargout = Programa1_OutputFcn(hObject, eventdata, handles) %%%%%%%%%%%%%%%%%%%%%%%%%%

varargout{1} = handles.output;

function edit3_Callback(hObject, eventdata, handles) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

V=get(hObject,'String'); handles.edit3=V; %para guardar en matlab guidata(hObject,handles); %para respaldo en guide

% --- Executes during object creation, after setting all properties.

function edit3_CreateFcn(hObject, eventdata, handles) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if ispc && isequal(get(hObject,'BackgroundColor'), get (0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end

function edit4_Callback(hObject, eventdata, handles) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

V=get(hObject,'String'); W=str2double(V); handles.edit4=W; %para guardaren matlab guidata(hObject,handles); %para respaldo en guide

% --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get (0,'defaultUicontrolBackgroundColor'))

function pushbutton4_Callback(hObject, eventdata, handles) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f1=handles.edit3; syms x x1=-10:0.1:10; y1=subs(f1,x,x1); plot(x1,y1),grid;