Need to graph integral on GUI?
1 view (last 30 days)
Show older comments
I'm creating a GUI that graphs the integral of a function, and allows the user to choose the left and right bounds on the x axis. Everything is pretty much set, but I need to figure out how to make the shading show up based on the user inputs for the bounds when I run the GUI. Here is my code for the function I wrote:
%calcIntegralF.m
%This script calculates the integral of a sine curve with a trapezoidal method.
function calcIntegralF(handles)
%%tabulate function for plotting
xmin=0;
xmax=2*pi;
Nx=100;
x=linspace(xmin,xmax,Nx);
y=sin(x);
%%tabulate function on finer grid for integration and filling
Nf=300;
xleft=0.3;
xright=2*pi/3;
xf=linspace(xleft,xright,Nf);
yf=sin(xf);
%%plot curve with area defined by (xf,yf) shaded
plot(handles.axes1,x,y)
deltax=xf(2)-xf(1);
Integ=0;
for ix=1:Nf-1
Integ=Integ+0.5*(yf(ix+1)+yf(ix))*deltax;
end
xlabel(handles.axes1,'x')
ylabel(handles.axes1,'y')
%%Display value of integral
set(handles.text5,'String',num2str(Integ))
Here is my code for the GUI:
function varargout = ch10prob5(varargin)
% CH10PROB5 MATLAB code for ch10prob5.fig
% CH10PROB5, by itself, creates a new CH10PROB5 or raises the existing
% singleton*.
%
% H = CH10PROB5 returns the handle to a new CH10PROB5 or the handle to
% the existing singleton*.
%
% CH10PROB5('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in CH10PROB5.M with the given input arguments.
%
% CH10PROB5('Property','Value',...) creates a new CH10PROB5 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ch10prob5_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ch10prob5_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 ch10prob5
% Last Modified by GUIDE v2.5 02-Apr-2013 14:59:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ch10prob5_OpeningFcn, ...
'gui_OutputFcn', @ch10prob5_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 ch10prob5 is made visible.df
function ch10prob5_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 ch10prob5 (see VARARGIN)
% Choose default command line output for ch10prob5
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ch10prob5 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
calcIntegralF(handles)
% --- Outputs from this function are returned to the command line.
function varargout = ch10prob5_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 slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
xleft=get(handles.slider1,'Value');
xleftstr=num2str(xleft);
set(handles.edit3,'String',xleftstr);
calcIntegralF(handles)
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
xright=get(handles.slider2,'Value');
xrightstr=num2str(xright);
set(handles.edit4,'String',xrightstr);
calcIntegralF(handles)
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
xleftstr=get(handles.edit3,'String');
xleft=str2double(xleftstr);
set(handles.slider1,'Value',xleft);
calcIntegralF(handles)
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
xrightstr=get(handles.edit4,'String');
xright=str2double(xrightstr);
set(handles.slider2,'Value',xright);
calcIntegralF(handles)
% --- 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'))
set(hObject,'BackgroundColor','white');
end
I'm still relatively new to MATLAB, so it is very possible that there are some minor errors in my script that are causing the curve to not shade in when I run the program. I would appreciate if someone could help me with this, whether the issue is in the function code or the GUI code. Thank you!
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!