How to let the user add a condition to the code using the console?

1 回表示 (過去 30 日間)
Riccardo Spader
Riccardo Spader 2021 年 7 月 30 日
回答済み: Ananya Tewari 2021 年 8 月 3 日
Hi everyone i'm an Italian guy, i'm so i make pasta and pizza better than i can use MATLAB, so i need your help.
I'm looking for making a code that solve a system of equations where the equations are given by the user who writes all the equations in the command prompt, if the user writes: "(10*a+b)*d" and "c+11*f" then (10*a+b)*d == c+11*f is a condition written inside an if function, and i have a big problem with the input system part .
Here my attempt:
close all
clc
clear
condition11= input('dump here the first equation', 's');
condition12= input('dump here the result of the first equation', 's');
%lines of code that gives to the variables a value
if condition11 == condition12
% do this ....
end
I tried to use the input function for my purpose but it didn't work at all since input wants numbers, variables or strings and i need to put generic equations as equations and not as strings because the if function will compare two strings that will be always different eachother, and not verify if the equation is verified.
I also tried to input using scr2func but it writes a handle function and the if can't compare two handle functions.
Can you help me, please?
  1 件のコメント
Eike Blechschmidt
Eike Blechschmidt 2021 年 7 月 30 日
編集済み: Eike Blechschmidt 2021 年 7 月 30 日
If the conditions can actually be calculated you could do something like
if eval(condition11) == eval(condition12)

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

回答 (1 件)

Ananya Tewari
Ananya Tewari 2021 年 8 月 3 日
Hi,
I understand you want to take equations as user input and solve the system of equation further.
We can take leverage of symbolic toolbox to create symbolic equations and solve them. Here is an example for taking equations as user inputs and converting it to symbolic equation using str2sym function.
syms a b d c f
str1 = input('Enter an equation in a b d: ','s') ;
str2 = input('Enter an equation in c f: ','s') ;
f1 = str2sym(str1) ;
f2 = str2sym(str2) ;

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by