MATLAB: Integrating input variable function differential equationsintegrationMATLAB how can i input a user defined variable/ quadratic equation and integrate it? what i am trying to do is ask user for the equation in then integrate it, (no limit) and give the ans Best Answer This is how I would do it:Integrating the quadratic without integration limits and displaying the result:prompt = {'x^2 Coefficient', 'x Coefficient', 'Constant'};default_ans = {'0','0','0'};dlg_title = 'Quadratic Equation';num_lines = 1;valc = inputdlg(prompt, dlg_title, num_lines, default_ans);vals = cell2mat(valc);valn = str2num(vals);qcf = valn(1:3);qint = polyint(qcf.');CreateStruct.Interpreter = 'tex';CreateStruct.WindowStyle = 'modal';msgbox(sprintf('Integral of: \n%.2f\\cdotx^2 %+.2f\\cdotx %+.2f = \n%.2f\\cdotx^3 %+.2f\\cdotx^2 %+.2f\\cdotx %+.2f', [qcf.' qint]),'Value',CreateStruct)Integrating the quadratic with limits and displaying the results of the integration:prompt = {'x^2 Coefficient', 'x Coefficient', 'Constant', 'Lower Integration Limit','Upper Integration Limit'};default_ans = {'0','0','0','0','0'};dlg_title = 'Quadratic Equation';num_lines = 1;valc = inputdlg(prompt, dlg_title, num_lines, default_ans);vals = cell2mat(valc);valn = str2num(vals);qcf = valn(1:3);int_lim = valn(4:5);qint = polyint(qcf.');int_val = diff(polyval(qint, [int_lim(1),int_lim(2)]));CreateStruct.Interpreter = 'tex';CreateStruct.WindowStyle = 'modal';msgbox(sprintf('Integral of %.2f\\cdotx^2 %+.2f\\cdotx %+.2f from %.2f to %.2f = %.3f', [qcf.' int_lim.' int_val]),'Value',CreateStruct)----------EDIT — Added the integration without limits. Related SolutionsMATLAB: How to get functions out of inputdlg I would do something like this, using str2func and vectorize: questions = {'Enter a function of ‘x’','Enter a lower limit','Enter an upper limit'};prompt = inputdlg(questions)equation = str2func(['@(x) ',vectorize(prompt{1})])lower_limit = str2num(prompt{2})upper_limit = str2num(prompt{3})P=integral(equation,lower_limit,upper_limit)Experiment to get different results. MATLAB: Request user input and using that ans for the functions >> val = str2double(answer{1})val = 3 Related QuestionNumerical integration using cumtrapzTake time from user !Solving a simple integral with input equation from user“ServerID must be a string” problemHelp with integral2 calculatorSeparating arguments by line break
Best Answer