Question Method Newton-Rhapson

1 view (last 30 days)
Jessé
Jessé on 27 Jul 2015
Answered: Walter Roberson on 27 Jul 2015
function varargout = Interface(varargin)
% Interface MATLAB code for Interface.fig
% Interface, by itself, creates a new Interface or raises the existing
% singleton*.
%
% H = Interface returns the handle to a new Interface or the handle to
% the existing singleton*.
%
% Interface('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in Interface.M with the given input arguments.
%
% Interface('Property','Value',...) creates a new Interface or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Interface_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Interface_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 Interface
% Last Modified by GUIDE v2.5 27-Jul-2015 14:54:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Interface_OpeningFcn, ...
'gui_OutputFcn', @Interface_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
clc
clear all
% End initialization code - DO NOT EDIT
% --- Executes just before Interface is made visible.
function Interface_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 Interface (see VARARGIN)
% Choose default command line output for Interface
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Interface wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Interface_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;
% --- Executes on button press in cmdCalcular.
function cmdCalcular_Callback(hObject, eventdata, handles)
% hObject handle to cmdCalcular (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%manejo de excepciones con try y catch
try
%Declaracion de variables. Se declara X como variable simbolica para poder utilizarla dentro de una funcion
%y no solo como cadena de texto
funcion=get(handles.txtFuncion,'string');
x0=str2double(get(handles.txtX0,'string'));
porcentajeError=str2double(get(handles.txtPorcentajeError,'string'));
syms x
iteracion=0;
errorCalculado=100;
f=sym(funcion);
derivada=diff(f,x);
%limpar tabla antes de mostrar resultado
set(handles.tabla,'Data',{})
%Comprobando que la derivada no sea cero.Casocontrario mostrar un mensaje
%que no hay raiz p.e se ingreso uan constante en lugar de una funcion de x
if derivada==0
%limpar tabla, grafico en caso de que antes se haya graficado uan
%funcion
hold off
cla
set(handles.tabla,'Data',{})
set(handles.txtRaiz,'string','Nao existe raiz neste intervalo');
else
%Iteraciones sucesivas para una mejor aproximacion de la raiz por el metodo
%N-R
while errorCalculado>porcentajeError
fx=subs(f,x0);
dx=subs(derivada,x0);
x1=x0-(fx/dx);
errorCalculado=abs(((x1-x0)/x1)*100);
%mostrara datos en tabla
valores = {iteracion x0 x1 errorCalculado};
temp=get(handles.tabla,'data');
valoresNuevos=[valores;temp];
set(handles.tabla,'Data',valoresNuevos)
x0=x1;
iteracion=iteracion+1;
end
%Mostrando respuesta en textbox con formato coma flotante a 16 cifras decimales
respuesta=sprintf('%0.16f',x1);
set(handles.txtRaiz,'string',respuesta);
%Grafica de la funcion
hold off
handles.axes1=ezplot(f);
grid on;
hold on;
handles.axes1=plot(x1,subs(f,respuesta),'r*');
zoom on
end
catch
msgbox('Ocorreu um erro! Verifique se introduziu os dados corretamente e de forma adequada!','Error','error')
end
% --- Executes on button press in cmdlimpar.
function cmdLimpar_Callback(hObject, eventdata, handles)
% hObject handle to cmdLimpar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%limpar area de grafico
cla
%limpar tabla
set(handles.tabla,'Data',{})
%limpar textboxs
set(handles.txtFuncion,'string','');
set(handles.txtX0,'string','');
set(handles.txtPorcentajeError,'string','');
set(handles.txtRaiz,'string','');
function txtFuncion_Callback(hObject, eventdata, handles)
% hObject handle to txtFuncion (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 txtFuncion as text
% str2double(get(hObject,'String')) returns contents of txtFuncion as a double
% --- Executes during object creation, after setting all properties.
function txtFuncion_CreateFcn(hObject, eventdata, handles)
% hObject handle to txtFuncion (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 txtX0_Callback(hObject, eventdata, handles)
% hObject handle to txtX0 (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 txtX0 as text
% str2double(get(hObject,'String')) returns contents of txtX0 as a double
% --- Executes during object creation, after setting all properties.
function txtX0_CreateFcn(hObject, eventdata, handles)
% hObject handle to txtX0 (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 txtPorcentajeError_Callback(hObject, eventdata, handles)
% hObject handle to txtPorcentajeError (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 txtPorcentajeError as text
% str2double(get(hObject,'String')) returns contents of txtPorcentajeError as a double
% --- Executes during object creation, after setting all properties.
function txtPorcentajeError_CreateFcn(hObject, eventdata, handles)
% hObject handle to txtPorcentajeError (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 txtRaiz_Callback(hObject, eventdata, handles)
% hObject handle to txtRaiz (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 txtRaiz as text
% str2double(get(hObject,'String')) returns contents of txtRaiz as a double
% --- Executes during object creation, after setting all properties.
function txtRaiz_CreateFcn(hObject, eventdata, handles)
% hObject handle to txtRaiz (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 sobre_Callback(hObject, eventdata, handles)
% hObject handle to sobre (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
sair
% --------------------------------------------------------------------
function instrucoes_Callback(hObject, eventdata, handles)
% hObject handle to instrucoes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
instrucoes
% --------------------------------------------------------------------
function sair_Callback(hObject, eventdata, handles)
% hObject handle to sair (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close
Could anyone help me ? I need you to print the gui the result of the derivative . Equal to the result of the root.

Answers (1)

Walter Roberson
Walter Roberson on 27 Jul 2015
Change
dx=subs(derivada,x0);
to
dx=subs(derivada,x0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!