フィルターのクリア

Should I insert tab for all lines of fsolve function?

1 回表示 (過去 30 日間)
farzad
farzad 2020 年 3 月 5 日
編集済み: Steven Lord 2020 年 3 月 5 日
Hi All
for solving a group of nonlinear equations with Fsolve, should I insert a tab for the equation lines that are under function definition ? because with or without the code works differently
  2 件のコメント
darova
darova 2020 年 3 月 5 日
What does it mean?
farzad
farzad 2020 年 3 月 5 日
which one of the two is coorect :
function myfun(x):
global A
a*x + y^4 +sin(x)-6
or
function myfun(x):
global A
a*x + y^4 +sin(x)-6
or
function myfun(x):
global A
a*x + y^4 +sin(x)-6

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

採用された回答

Steven Lord
Steven Lord 2020 年 3 月 5 日
編集済み: Steven Lord 2020 年 3 月 5 日
which one of the two is coorect :
None of the three are correct, but it's not due to different levels of indentation. Indentation doesn't matter to MATLAB in many (or perhaps any; I can't think of any situation offhand where it does) situations. [It can make your code more readable by humans, but MATLAB doesn't need indentation to "read" your code.] All of your samples have the same problems, so I'll just focus on the first. Some of what I'm going to call out is covered on this documentation page. It would probably be a good idea for you to review that page.
function myfun(x):
global A
a*x + y^4 +sin(x)-6
For use with fsolve your function must return an output argument. Yours does not.
The use of : to end the function declaration line is incorrect. Remove the colon.
Using a global variable is not an error, but it is a programming pattern that is discouraged. ANY function with access to the global workspace (i.e. any function) can affect how your function operates, potentially without your knowledge. This can lead to problems that are difficult or impossible to debug.
The third line of your function uses variables that do not exist in the workspace of your myfun function. The variables A and a are not the same variable; case matters. In addition, you've never defined a variable y so that will most likely throw an error.
The third line of your function doesn't assign the result of that calculation to any variable. It should probably assign that result to the output variable you added to the function declaration line (see my first comment.)
If you don't mind me saying so, it seems from your code like you're relatively new to using MATLAB. If so I recommend taking the free MATLAB Onramp tutorial to familiarize yourself with the essentials of MATLAB. I'm not sure it covers writing functions (I haven't taken it myself; I probably should one of these days to see what it actually covers) but it should help you become familiar with and/or more comfortable using MATLAB.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by