asking for a polynomial expression in an input

2 Ansichten (letzte 30 Tage)
Byron
Byron am 2 Mai 2023
Bearbeitet: John D'Errico am 2 Mai 2023
I am building a calculator app for an assignment, and got stuck on the fact that the equation I am calculating evaluates the gradient of the inputs. when the inputs are constant the gradent is 0 (obviously). For the calculator to work I would like the user to be able to input a polynomial expression. Any help or hints are apprecited, I am very stuck and confused.

Antworten (1)

John D'Errico
John D'Errico am 2 Mai 2023
Bearbeitet: John D'Errico am 2 Mai 2023
Can you type in a polynomial? Why not? You type it anyway. Just use input, which can return the result as a string. Or, use a dialog box.
help input
Copyright 2015-2020 The MathWorks, Inc. Documentation for input doc input Folders named input shadows/input
help inputdlg
INPUTDLG Input dialog box. ANSWER = INPUTDLG(PROMPT) creates a modal dialog box that returns user input for multiple prompts in the cell array ANSWER. PROMPT is a cell array containing the PROMPT strings. INPUTDLG uses UIWAIT to suspend execution until the user responds. ANSWER = INPUTDLG(PROMPT,NAME) specifies the title for the dialog. ANSWER = INPUTDLG(PROMPT,NAME,NUMLINES) specifies the number of lines for each answer in NUMLINES. NUMLINES may be a constant value or a column vector having one element per PROMPT that specifies how many lines per input field. NUMLINES may also be a matrix where the first column specifies how many rows for the input field and the second column specifies how many columns wide the input field should be. ANSWER = INPUTDLG(PROMPT,NAME,NUMLINES,DEFAULTANSWER) specifies the default answer to display for each PROMPT. DEFAULTANSWER must contain the same number of elements as PROMPT and must be a cell array of strings. ANSWER = INPUTDLG(PROMPT,NAME,NUMLINES,DEFAULTANSWER,OPTIONS) specifies additional options. If OPTIONS is the string 'on', the dialog is made resizable. If OPTIONS is a structure, the fields Resize, WindowStyle, and Interpreter are recognized. Resize can be either 'on' or 'off'. WindowStyle can be either 'normal' or 'modal'. Interpreter can be either 'none' or 'tex'. If Interpreter is 'tex', the prompt strings are rendered using LaTeX. Examples: prompt={'Enter the matrix size for x^2:','Enter the colormap name:'}; name='Input for Peaks function'; numlines=1; defaultanswer={'20','hsv'}; answer=inputdlg(prompt,name,numlines,defaultanswer); options.Resize='on'; options.WindowStyle='normal'; options.Interpreter='tex'; answer=inputdlg(prompt,name,numlines,defaultanswer,options); See also DIALOG, ERRORDLG, HELPDLG, LISTDLG, MSGBOX, QUESTDLG, TEXTWRAP, UIWAIT, WARNDLG . Documentation for inputdlg doc inputdlg
Next, how can you convert the string into a symbolix expression?
help str2sym
STR2SYM Evaluate string representing symbolic expression. str2sym(S) evaluates S as a symbolic expression, where S can be a string, character vector, or cell array of character vectors. str2sym(S) behavior: Only executes functions on path when converting the string. Does not consider variables and values from workspace. Does not add variables in the input to the workspace. Does not support programming statements like IF. Treats assignments ('=') as equations ('=='). For numeric strings, use SYM for exact symbolic numbers and VPA for floating-point numbers. See also SUBS, SYM, SYMFUN, SYMS, SYMVAR, VPA. Examples: >> str2sym("b^2 + pi/2 + sin(x) = x^3 - 2") ans = b^2 + pi/2 + sin(x) == x^3 - 2 >> str2sym('int(x^3,x)') ans = x^4/4 >> str2sym('y(0) = 42') ans = y(0) == 42 >> str2sym({'42', 'a', 'y(0)'}) ans = [ 42, a, y(0)] >> str2sym('[42, a, y(0)]') ans = [ 42, a, y(0)] Documentation for str2sym doc str2sym
Learn to use the tools in MATLAB. They are there.

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by