You are on page 1of 19

UNIVERSIDAD CATÓLICA DE SANTA MARÍA

ESCUELA PROFESIONAL DE INGENIERÍA MECÁNICA, MECÁNICA


ELÉCTRICA Y MECATRÓNICA
CÓDIGO :
: MECANICA COMPUTACIONAL II GUÍA DE LABORATORIO NO 02
ASIGNATURA
Docente (s):
SEGUNDA FASE Ing.Cuadros
Matlab uso de GUI en método del trapecio para hallar áreas
Fecha: 2019.05.14.

Nombres y apellidos : Calle Apaza Omar Gabriel


Código : 2017220821
Grupo : 04

1. Objetivos:
 Implementar diversas fórmulas que permiten calcular polinomios de
interpolación, según las diversas metodologías estudiadas:
interpolación lineal, interpolación cuadrática, interpolación por el
polinomio de Newton e interpolación por el polinomio de Lagrange.
 Graficar polinomios de interpolación de diversas funciones y con
diferentes mallas de modo de poder apreciar en el computador los
errores de interpolación.

2. Guide:
3. Codificación:

function varargout = InterpolacionTovarSaniz(varargin)


% INTERPOLACIONTOVARSANIZ MATLAB code for
InterpolacionTovarSaniz.fig
% INTERPOLACIONTOVARSANIZ, by itself, creates a new
INTERPOLACIONTOVARSANIZ or raises the existing
% singleton*.
%
% H = INTERPOLACIONTOVARSANIZ returns the handle to a new
INTERPOLACIONTOVARSANIZ or the handle to
% the existing singleton*.
%
%
INTERPOLACIONTOVARSANIZ('CALLBACK',hObject,eventData,handles,...)
calls the local
% function named CALLBACK in INTERPOLACIONTOVARSANIZ.M with
the given input arguments.
%
% INTERPOLACIONTOVARSANIZ('Property','Value',...) creates a
new INTERPOLACIONTOVARSANIZ or raises the
% existing singleton*. Starting from the left, property
value pairs are
% applied to the GUI before
InterpolacionTovarSaniz_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to
InterpolacionTovarSaniz_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows
only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help


InterpolacionTovarSaniz

% Last Modified by GUIDE v2.5 16-Oct-2018 10:18:50

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn',
@InterpolacionTovarSaniz_OpeningFcn, ...
'gui_OutputFcn',
@InterpolacionTovarSaniz_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 InterpolacionTovarSaniz is made


visible.
function InterpolacionTovarSaniz_OpeningFcn(hObject, eventdata,
handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to InterpolacionTovarSaniz
(see VARARGIN)

% Choose default command line output for InterpolacionTovarSaniz


handles.output = hObject;

% Update handles structure


guidata(hObject, handles);
global radioboton;
radioboton = 0;
% UIWAIT makes InterpolacionTovarSaniz wait for user response (see
UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = InterpolacionTovarSaniz_OutputFcn(hObject,
eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

function funcion_Callback(hObject, eventdata, handles)


% hObject handle to funcion (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of funcion as text


% str2double(get(hObject,'String')) returns contents of
funcion as a double

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


properties.
function funcion_CreateFcn(hObject, eventdata, handles)
% hObject handle to funcion (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'))
set(hObject,'BackgroundColor','white');
end
function datos_Callback(hObject, eventdata, handles)
% hObject handle to datos (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of datos as text


% str2double(get(hObject,'String')) returns contents of
datos as a double

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


properties.
function datos_CreateFcn(hObject, eventdata, handles)
% hObject handle to datos (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'))
set(hObject,'BackgroundColor','white');
end

function datointerpolar_Callback(hObject, eventdata, handles)


% hObject handle to datointerpolar (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of datointerpolar


as text
% str2double(get(hObject,'String')) returns contents of
datointerpolar as a double
% --- Executes during object creation, after setting all
properties.
function datointerpolar_CreateFcn(hObject, eventdata, handles)
% hObject handle to datointerpolar (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'))
set(hObject,'BackgroundColor','white');
end

function aprox_Callback(hObject, eventdata, handles)


% hObject handle to aprox (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of aprox as text


% str2double(get(hObject,'String')) returns contents of
aprox as a double

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


properties.
function aprox_CreateFcn(hObject, eventdata, handles)
% hObject handle to aprox (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'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in calcular.


function calcular_Callback(hObject, eventdata, handles)

global radioboton;

if radioboton == 0
%lineal
dato = 0;
f = get(handles.funcion,'string');

vect = str2num(get(handles.datos,'string'));
x0 = vect(1);
x1 = vect(2);
xI = str2double(get(handles.datointerpolar,'string'));
x =x0; fx0 = eval(f);
x =x1; fx1 = eval(f);
x =xI; fxI = eval(f);
por = '%';
aprox = fx0 + ((fx1-fx0)/(x1-x0))*(xI-x0);
set(handles.aprox,'string',aprox);
%fprintf('Aproximacion = %.6f\n',aprox);
set(handles.valorreal,'string',fxI)
error = 100- (aprox*100)/fxI;
set(handles.error,'string',error)
%fprintf('Error = %.2f%c\n',error,por);
v(1,1)=fx0;
v(2,1)=fx1;
v(2,2)=(fx1-fx0)/(x1-x0);
set(handles.uitable1,'Data',v);
axes(handles.axes1)
fplot(f,[x0, x1],'r');
grid on
hold on
plot(xI,aprox,'*b')
elseif radioboton == 1
%cuadratica
dato = 1;
f = get(handles.funcion,'string');

vect = str2num(get(handles.datos,'string'));
a = vect(1);
b = vect(2);
c = vect(3);
d = str2double(get(handles.datointerpolar,'string'));

x=a; fa=eval(f);
x=b; fb=eval(f);
x=c; fc=eval(f);
x=d; vreal = eval(f);
b3 = (fc-fb)/(c-b);
b0=fa;
b1=(fb-fa)/(b-a);
b2=(((fc-fb)/(c-b))-((fb-fa)/(b-a)))/(c-a);
a0=b0-(b1*a)+(b2*a*b);
a1=b1-(b2*a)-(b2*b);
a2=b2;
f2=[num2str(a0),'+',num2str(a1),'*x+',num2str(a2),'*x.^2'];
x=d;
fd=eval(f2);
error = 100 - ((fd*100)/vreal);
set(handles.aprox,'string',fd);
set(handles.valorreal,'string',vreal);
set(handles.error,'string',error);
%fprintf('La aproximacion es %-3.6f\n',fd)
axes(handles.axes1);
x=linspace(a,c,1000);
y=eval(f);

plot(x,y,'r'), hold on;


y2=eval(f2);
plot(x,y2,'b'), hold on;
plot(d,fd,'sk','MarkerFaceColor','k')
hold on
plot(a,fa,'og',b,fb,'og',c,fc,'og')
v(1,1)=fa;
v(2,1)=fb;
v(3,1)=fc;
v(2,2)=b1;
v(3,2)=b3;
v(3,3)=b2;
set(handles.uitable1,'Data',v);

elseif radioboton == 2
%cubica
dato = 2;
f = get(handles.funcion,'string');
fil = length(str2num(get(handles.datos,'string')));
%fil = input('ingrese cantiad de elementos: ');
vector = str2num(get(handles.datos,'string'));
%inicializando matriz
col = fil;
for i=1:fil
for j=1:col
mat(i,j)=-100;
end
end
%generando vector de entrada y matriz de datos iniciales
for i=1:fil
%fprintf('- ingrese elemento %d : ' ,i-1);
vect(i) = vector(i);
x = vect(i); fx = eval(f);
mat(i,1) = fx;
end
%disp(vect);
%disp(mat);

xI = str2double(get(handles.datointerpolar,'string'));
%xI = input('ingrese el valor a intepolar: ');
%calculando b0,b1,b2....b(n-1)
a = 1;
for j=2:col
a=a+1;
for i=a:fil
mat(i,j)= ((mat(i,j-1))-(mat(i-1,j-1)))/(vect(i)-
vect(i-j+1));

end
end

for i=1:fil
for j=1:col
if (mat(i,j)==-100)
mat(i,j)= 0;
end
end
end

set(handles.uitable1,'Data',mat);
%visualizando matriz b
%apro= b0+b1(xI-x0)+
apro=0;
val=0;
cic=0;
for i=1:fil
b= mat(i,i);
acu=1;
if val~=0
cic=cic+1;
for j=1:cic
acu = acu * (xI-vect(j));
end
end
val=1
apro =apro+(b*acu)
end
%por='%';
set(handles.aprox,'string',apro);
%fprintf('Aproximacion = %.6f\n',apro);
x=xI; vreal = eval(f);
error = 100 - (apro*100)/vreal;
set(handles.error,'string',error);
set(handles.valorreal,'string',vreal)
%fprintf('Error = %.2f%c\n',error,por);

x=vector(1); fx0=eval(f);
x=vector(2); fx1=eval(f);
x=vector(3); fx2=eval(f);
x=vector(4); fx3=eval(f);
x=xI; vreal = eval(f);
b0 = fx0;
b1 = (fx1-fx0)/(vector(2)-vector(1));
z = (fx2-fx1)/(vector(3)-vector(2));
b2 = (z-b1)/(vector(3)-vector(1));
c = (fx3-fx2)/(vector(4)-vector(3));
x = (c-z)/(vector(4)-vector(2));
b3 = (xI-b2)/(vector(4)-vector(1));
t1 = (b1*(xI-vector(1)));
t2 = (b2*(xI-vector(1))*(xI-vector(2)));
t3 = (b3*(xI-vector(1))*(xI-vector(2))*(xI-vector(3)));
%fi = (b0) + +(b2*(xI-vector(1))*(xI-vector(2)))+(b3*(xI-
vector(1))*(xI-vector(2))*(xI-vector(3)));
%fi = [num2str(b0),'-',num2str(t1),'-
',num2str(t2),'*x+',num2str(t3),'*x.^3'];
%f2=[num2str(a0),'+',num2str(a1),'*x+',num2str(a2),'*x.^2'];
x= xI;
%fd = eval(fi)
axes(handles.axes1);
x=linspace(vect(1),vect(4)+4,1000);
y=eval(f);
% func = a + b*x+c*x^2+d*x^3
fun1 = mat(1,1);
fun2 = mat(2,2);
fun3 = mat(3,3);
fun4 = mat(4,4);
func= [num2str(fun1),'+',num2str(fun2),'*(x-
1)',num2str(fun3),'*(x.^2-5*x+4)+',num2str(fun4),'*(x.^3-
11*x.^2+34*x-24)'];

%func = mat(1,1) + mat(2,2)*x+ mat(3,3)*x^2+mat(4,4)*x^3;


plot(x,y,'r'), hold on;
x=vect(1):0.01:vect(4)+4;
y2=eval(func);
plot(x,y2,'b'), hold on;
x=xI;
fd = eval(func)
%xq1 = vector(1):0.01:vector(4);
%s = spline(x,y,xq1);

plot(xI,fd,'sk','MarkerFaceColor','k')
%plot(xq1,s,'-.')
hold on

plot(vector(1),fx0,'og',vector(2),fx1,'og',vector(3),fx2,'og',vect
or(4),fx3,'og')

else
dato = 3;
f = get(handles.funcion,'string');
vect = get(handles.datos,'string');
v = str2num(vect);
xI = str2double(get(handles.datointerpolar,'string'));
n = length(v);
x=xI; vreal = eval(f);

for i=1:n
x = v(i);
mat(i,1) = eval(f);
end

j=2;
acu=2;
i=2;

while j<=n

while i<=n
mat(i,j)= ((mat(i,j-1))-(mat(i-1,j-1)))/(v(i)-v(i-
j+1));
i= i+1;
end
j=j+1;
acu = acu+1;
i = acu;
end
set(handles.valorreal,'string',vreal);
set(handles.uitable1,'Data',mat);
acu=1;
fi = 0;
for i=1:n-1
acu = (xI-v(i))*acu;
fi =(mat(i+1,i+1)*acu) + fi;

end
aprox = mat(1,1) + fi;
set(handles.aprox,'string',aprox);
error = abs(100 - ((aprox*100)/vreal));

set(handles.error,'string',error);

end
radioboton = dato;
% --- Executes on button press in limpiar.
function limpiar_Callback(hObject, eventdata, handles)
cla(handles.axes1,'reset')

ini = [ 0' 0'];


set(handles.uitable1,'Data',ini);

% --- Executes on button press in salir.


function salir_Callback(hObject, eventdata, handles)
% hObject handle to salir (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
opc1 = questdlg('¿Desea cerrar el
programa?','Salir','Si','No','No');
if strcmp(opc1,'No')
return;
end
clear,clc,close all

function valorreal_Callback(hObject, eventdata, handles)


% hObject handle to valorreal (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of valorreal as


text
% str2double(get(hObject,'String')) returns contents of
valorreal as a double

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


properties.
function valorreal_CreateFcn(hObject, eventdata, handles)
% hObject handle to valorreal (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'))
set(hObject,'BackgroundColor','white');
end

function error_Callback(hObject, eventdata, handles)


% hObject handle to error (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of error as text


% str2double(get(hObject,'String')) returns contents of
error as a double
% --- Executes during object creation, after setting all
properties.
function error_CreateFcn(hObject, eventdata, handles)
% hObject handle to error (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'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in lineal.


function lineal_Callback(hObject, eventdata, handles)
% hObject handle to lineal (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
global radioboton
lineal.Value = true;
radioboton = 0;
%set(handles.aprox,'string',radioboton)
% Hint: get(hObject,'Value') returns toggle state of lineal

% --- Executes on button press in cuadratica.


function cuadratica_Callback(hObject, eventdata, handles)
% hObject handle to cuadratica (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
global radioboton
cuadratica.Value = true;
radioboton = 1;
%set(handles.aprox,'string',radioboton)
% Hint: get(hObject,'Value') returns toggle state of cuadratica

% --- Executes on button press in cubica.


function cubica_Callback(hObject, eventdata, handles)
% hObject handle to cubica (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
global radioboton
cubica.Value = true;
radioboton = 2;
%set(handles.aprox,'string',radioboton)
% Hint: get(hObject,'Value') returns toggle state of cubica

% --- Executes on button press in diferenciadividida.


function diferenciadividida_Callback(hObject, eventdata, handles)
% hObject handle to diferenciadividida (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
global radioboton
cubica.Value = true;
radioboton = 3;
% Hint: get(hObject,'Value') returns toggle state of
diferenciadividida
4. Pruebas y resultados:
5. Conclusiones:
 La interpolación sirve para aproximarnos a una función complicada a una
mas simple.
 Se aprendió a transformar ecuaciones matemáticas como la interpolación,
lineal, cuadrada, cubica y de diferencias divididas, para luego estimar los
valores de la aproximación de cada función según sus valores reales
obtenidos.
 La aproximación de cada punto en una función es mas precisa según el
método comenzando por la lineal hasta las diferencias divididas que son
estimaciones a puntos encontrados por las ecuaciones matemáticas.
 Se estableció el procedimiento para realizar el algoritmo de cada función.

You might also like