How can I find "d" in the following equation, using Matlab?

3 ビュー (過去 30 日間)
Kostas Kostas
Kostas Kostas 2018 年 2 月 7 日
コメント済み: Steven Lord 2018 年 2 月 7 日
rho=997;
mu=0.9;
sigma=71.8;
oh=0.003;
oh=mu/sqrt(rho*sigma*d);
i receive an error message that says
Undefined function or variable 'd'.
Error in Untitled (line 5) oh=mu/sqrt(rho*sigma*d);
and I have no clue what to do
Thank you

回答 (2 件)

Rik
Rik 2018 年 2 月 7 日
This is not a Matlab question. Learn the tool you are using before you attempt to solve problems with it. One of the many free online crash course on Matlab is OnRamp, offered by Mathworks.
Have a read here and here. It will greatly improve your chances of getting an answer and help you find your own solutions in the future.
As for you current problem: this is easily done by hand:
oh=mu/sqrt(rho*sigma*d);
sqrt(rho*sigma*d)=mu/oh;
rho*sigma*d=mu^2/oh^2;
d=mu^2/(oh^2*rho*sigma);
(you need to edit this solution if you want to use vectors as inputs)

Steven Lord
Steven Lord 2018 年 2 月 7 日
Nowhere in the code you have posted do you define a variable named d. From the error message you do not have a function named d accessible to MATLAB when you execute that code. When you try to compute the expression rho*sigma*d, MATLAB has no idea what value to use for d.
If you intended d to be a numeric value, you need to define it prior to using it in your code.
If you intended for d to be a parameter that you replace with a value later on, there are a couple of approaches you could use.
  1. You could write your code as a function instead of a script. This section of the documentation talks about the differences between scripts and functions.
  2. Because the expression for oh is simple enough you could write it as an anonymous function.
  3. If you have Symbolic Math Toolbox, you could define d to be a symbolic variable and use the subs function to replace it with a value when you need to generate a numeric result.
  2 件のコメント
Rik
Rik 2018 年 2 月 7 日
I noticed Kostas Kostas already defined oh, so this is essentially solving an equation, so rather than subs, I would point Kostas Kostas to solve.
Steven Lord
Steven Lord 2018 年 2 月 7 日
Yes, solve would work if d was defined as a symbolic variable and you wanted a symbolic solution. If d was a symbolic variable you could also use vpasolve.
If you wanted to solve it numerically, you could use fzero since it's one function in one unknown.
% Solve 5*x + sin(x) = 7
% Step 1: rewrite as 5*x + sin(x) - 7 = 0
% Step 2: define the function for the left side
f = @(x) 5*x + sin(x) - 7;
% Step 3: solve
x = fzero(f, 0)
% Step 4: check
shouldBeCloseToZero = f(x)

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by