integrating input variable function

조회 수: 6 (최근 30일)
mohammad reza
mohammad reza 2016년 7월 12일
댓글: Star Strider 2016년 7월 12일
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

채택된 답변

Star Strider
Star Strider 2016년 7월 12일
편집: Star Strider 2016년 7월 12일
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.
  댓글 수: 2
mohammad reza
mohammad reza 2016년 7월 12일
ya, but not numeric integration rather symbolic'
% f = input('please input load eq \n','s'); % first input as string than convert the string to the function % make sure to put @x (variable in the input string command) % fh = str2func(f)
this takes the user defined eq ...any type...but cant integrate symbolacally
Star Strider
Star Strider 2016년 7월 12일
We were typing at the same time.
I re-read your question and added a section that calculates the symbolic integral of the quadratic and displays the integrated result, without actually integrating it. This code does not create a function handle, but that is relatively easy to do if you need to.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

mohammad reza
mohammad reza 2016년 7월 12일
nice work it some what does the work but i need to create function handle...as i have to intregrate a user input 4 times and and each result has to be evaluated in different co ordinate values (arrays which will be also in a user defined)
but thanks for your help. i just wanted to see if there is any easy way.
  댓글 수: 1
Star Strider
Star Strider 2016년 7월 12일
To create a funciton handle, add str2func and sprintf calls:
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.');
intquad_fcn = str2func(sprintf('@(x) %f.*x.^3 + %f.*x.^2 + %f.*x + %f', qint))
intquad_fcn =
@(x)0.333333.*x.^3+1.000000.*x.^2+3.000000.*x+0.000000
It exists as the function in the code, so adding one line to test it:
intquad_val = intquad_fcn(5) % Test Line (Delete Later)
intquad_val =
81.6666e+000

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by