How do I plot a function using symbolic expressions?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I'm trying to plot any function that is given in a "Edit text" box in GUI, but I'm having issues while using syms x and then writing in the "Edit text" box a function such as "2x". I have tried to evaluate the string of the "Edit text" box as symbolic expression but it just does not work. Since I'm very unexperince with this software, I'm quite lost and confused, so any help is very useful!
This is the code:
% --- Outputs from this function are returned to the command line.
function varargout = intento1_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
syms x;
funcion = get(handles.edit1, 'String')
aux = get(handles.edit2, 'String')
aux2 = get(handles.edit3, 'String')
aux3 = get(handles.edit4, 'String')
funcionConv = str2sym(['@(x)', funcion])
fplot(funcionConv, [-10 10],'b', LineWidth=2)
if (aux ~= "")
punto = str2double(get(handles.edit2, 'String'))
limit0 = limit(funcion, x, punto)
elseif (aux2 ~= "")
puntoI = str2double(get(handles.edit3, 'String'))
puntoD = str2double(get(handles.edit4, 'String'))
limitI = limit(funcion, x, puntoI, "left")
limitD = limit(funcion, x, puntoD, "right")
elseif (aux3 ~= "")
infinito = str2double(get(handles.edit5, 'String'))
limitInf = limit(funcion, x, infinito)
end
And those are the errors:
Error using str2symInternal
Unable to convert string to symbolic expression:
L 1 (C 6): SYNER: Parse error at x: usage might be invalid MATLAB syntax.
Error in str2sym (line 53)
T = str2symInternal(S);
Error in intento1>pushbutton1_Callback (line 92)
funcionConv = str2sym(['@(x)', funcion])
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in intento1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)intento1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Related documentation
Error while evaluating UIControl Callback.
0 Commenti
Risposta accettata
Walter Roberson
il 21 Ott 2022
Modificato: Walter Roberson
il 21 Ott 2022
See symfun for creation of symbolic functions . You would first str2sym the expression without the @(x) and you would pass the result as the first parameter to symfun() and the list of variable names as the second parameter.
Or, if you have a fixed variable name then
syms x
functionConv(x) = str2sym(funcion);
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Entering Commands in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!