How to get functions out of inputdlg

1 回表示 (過去 30 日間)
Mason Smith
Mason Smith 2020 年 4 月 6 日
コメント済み: Star Strider 2020 年 4 月 6 日
questions = {'Enter a function','Enter a lower limit','Enter an upper limit'};
prompt = inputdlg(questions)
equation = str2num(prompt{1})
lower_limit = str2num(prompt{2})
upper_limit = str2num(prompt{3})
P=int(sym('equation'),lower_limit,upper_limit)
Hello everyone. My question is how do I keep the inputted equation (such as x.^2) as itself, instead of it changing to x. When I run the program below (you should be able to copy and paste it if you want to run it) the inputted function gets changed to x. Many thanks.

採用された回答

Star Strider
Star Strider 2020 年 4 月 6 日
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.
  2 件のコメント
Mason Smith
Mason Smith 2020 年 4 月 6 日
You are a life saver. Thank you so much!
Star Strider
Star Strider 2020 年 4 月 6 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by