Why does matlab give me the wrong answer?
古いコメントを表示
why?

2 件のコメント
Rik
2019 年 9 月 14 日
3-2 is 1, which is greater than 0. How is this result wrong?
Linus Heierhoff
2019 年 9 月 14 日
回答 (1 件)
Priyanshu Mishra
2019 年 9 月 18 日
Although this answer is not wrong, but for getting conditions on the solution, set ‘ReturnConditions’ to true. It will return any parameter in the solution and conditions on the solution. You can try following code:
clear all; close all; clc;
syms x;
eqn = x-2 >= 0;
assume (eqn)
S= solve (eqn,x,'ReturnConditions',true)
S.conditions
5 件のコメント
Rik
2019 年 9 月 18 日
Why are you suggesting the use of clear all and close all? Clearing everything is very rarely needed (as mlint warns) and this code doesn't open any figures, so there aren't any that might cause conflicts here. Because you are printing results the use of clc is not a bad idea.
I would suggest clearvars,clc as the top line instead.
Bruno Luong
2019 年 9 月 18 日
clear and close are often good practice when people copy/past code and try it.
This is completely safe and appropriate when posting reply in ANSWER.
Rik
2019 年 9 月 18 日
While I mostly agree with you, I'm not convinced the same can be said for clear all. I think this encourages cargo cult programming. If you use the things I suggest, you show the intended effect much more clearly, and you imply that you should not be doing things just because you see them often.
People should be aware what these functions do. Using them as a trio like this discourages this, because it turns them into a keyword, to be used at the start of all scripts and functions.
Priyanshu Mishra
2019 年 9 月 20 日
Hi Rik, I kind of agree with you. It completely depends upon the user whether he wants to close all the windows or not. I have written this code from the scratch and I just used those commands for my convenience. The user can start from line 2 .
Rik
2019 年 9 月 20 日
My problem is that many inexperienced users always see those together and don't understand what they do, but assume they are all required to set up a working script. I have no problem with people using them (I use them myself regularly), but I do think we should be selective in what we put in our answers, because like it or not, we are teaching people best practices every time we post an answer.
カテゴリ
ヘルプ センター および File Exchange で Code Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!