Problem plotting function in GUI
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone, this is the first time I post here so if I sound like a newbie is because I am. I have a problem when trying to plot a function in GUI so I decided to do some tests in order to figure it out. I'm creating a non-linear equation solver using Newton-Raphson Method and this is how the UI looks (is in Spanish but is quite intuitive):
Funcion = The function the user wants to solve. Stored in the variable 'funcion' and then I use the inline() function in order to get a function that I can use in order to compute values (stored in the variable f). When he clicks on "Click aqui para mostrar la derivada", the program derivates the function the user wants to solve in order to use it in the formula. Valor inicial = Initial value. "Elija el metodo" = The user can choose one of two methods, fixed number of iterations or tolerance (hope this is clear). "Calcular" = The method executes and finds the value wanted.
I haven't implemented the table yet so I see the answer in the command window. Until this point, everything works smoothly, the root is stored in the variable 'x1'. The problem begins when I click the push button called "Graficar respuesta". What it does is to plot the function and a circle around the point 'x1' which represents one root of the function, at least that's what is supposed to do. The code is:
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global x1;
global funcion;
global f;
if x1==0
x1=x1+1;
elseif x1<0
x1=-x1;
end
% axes(handles.axes2);
%Imagen
figure (1);
x=-5*x1:0.001:5*x1;
plot(x,funcion,'Color','red'), title('Grafica de la funcion'), xlabel('Eje X'), ylabel('Eje Y'), grid;
hold on;
scatter(x1,f(x1));
But I get the next error:
Error using plot
Error in color/linetype argument.
Error in newton>pushbutton3_Callback (line
196)
plot(x,funcion,'Color','red'), title('Grafica
de la funcion'), xlabel('Eje X'), ylabel('Eje
Y'), grid;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in newton (line 18)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)newton('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
What am I doing wrong? I would really appreciate any help, please. Thanks in advance.
6 Kommentare
Stephen23
am 1 Jul. 2018
Avoid using global variables, evalin, and assignin. For callback functions use guidata (for GUIDE) or nested functions (if you are writing your own code). For non-GUI code pass variables as input/output arguments.
You will find plenty of simple examples on this forum, e.g.:
Using guidata is very simple:
function foobar(hObject, eventdata, handles)
...
... handles.X ... % access field X
handles.Y = ... % define field Y
...
guidata(hObject,handles) % save any new data
Antworten (1)
Rik
am 29 Jun. 2018
Using the handles struct is very easy. It may seem hard to learn good practices, but it is much harder to unlearn bad practices. The code below works on my system (R2018a). You should really pay attention to the m-lint warnings. Replacing an old function like inline with anonymous functions is a good place to start. In your call to that figure 1, I would add a clf(1), especially since you have hold turned on.
function funcion_Callback(hObject, ~, handles)
syms x;
funcion=get(handles.funcion,'string');
f=str2func(['@(x)' funcion]);
df=symfun(diff(f,x),x)
set(handles.derivada,'string',char(df));
handles.funcion=funcion;
handles.df=df;
handles.f=f;
guidata(hObject,handles)
function pushbutton3_Callback(hObject, ~, handles)
x1=handles.x1;%global x1;
%funcion=handles.funcion;%global funcion;
f=handles.f;%global f;
if x1==0
x1=x1+1;
elseif x1<0
x1=-x1;
end
% axes(handles.axes2);
%Imagen
figure (1);
x=-5*x1:0.001:5*x1;
plot(x,f(x),'Color','red'), title('Grafica de la funcion'), xlabel('Eje X'), ylabel('Eje Y'), grid;
hold on;
scatter(x1,f(x1));
6 Kommentare
Diego Guisasola Plejo
am 4 Jul. 2018
Bearbeitet: Diego Guisasola Plejo
am 4 Jul. 2018
Rik
am 4 Jul. 2018
You could replace
plot(x,f(x),'Color','red')
by
plot(x,arrayfun(f,x),'Color','red')
%untested, you might need to add 'UniformOutput',0 to arrayfun
That way the function is applied to each element separately, which makes it slower, but will prevent odd errors in cases of powers, multiplications, and divisions.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!