simple question function file syntax

2 ビュー (過去 30 日間)
B
B 2015 年 5 月 5 日
回答済み: Walter Roberson 2015 年 5 月 6 日
function[root,ea,iter]=bisectt(func,xl,xu,es,maxit,varargin)
if nargin <3,
disp('error')
end
test=func(xl,varargin{:})*func(xu,varargin{:});
if test>0,
disp('error')
end
if test>0,error('no sign change'),end
if nargin<4|isempty(es), es=0.0001;end
if nargin<5|isempty(maxit), maxit=50;end
iter = 0; xr = xl; ea = 10;
while (1)
xrold = xr;
xr = (xl + xu)/2;
iter = iter + 1;
if xr ~= 0,ea = abs((xr - xrold)/xr) * 100;end
test = func(xl,varargin{:})*func(xr,varargin{:});
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
ea = 0;
end
if ea <= es | iter >= maxit,break,end
end
root = xr;
I need to create another function file to calculate f(xl), f(xr), f(xu) how can i do that? the function is:
sqrt((9.81*x)/0.25)*tanh(sqrt((9.81*0.25)/x)*4)
thanks for your help!

採用された回答

Michael Haderlein
Michael Haderlein 2015 年 5 月 5 日
You can use anonymous functions:
f=@(x) sqrt((9.81*x)/0.25).*tanh(sqrt((9.81*0.25)./x)*4);
Then, f(xr) will return the respective value and f([xl xr xu]) will return all 3 values.
  2 件のコメント
B
B 2015 年 5 月 5 日
I'm still getting an erro. the syntax I'm using is wrong. can you provide me with the correct function file for sqrt((9.81*x)/0.25).*tanh(sqrt((9.81*0.25)./x)*4) that will evaluate the three variables xl,xu,xr
Michael Haderlein
Michael Haderlein 2015 年 5 月 6 日
Ah, you want an extra file for that. Then the syntax is just
function res=myfunctionname(x)
res=sqrt((9.81*x)/0.25).*tanh(sqrt((9.81*0.25)./x)*4);
Put this in a file named myfunctionname.m, that's all.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 5 月 6 日
f=@(x) sqrt((9.81*x)/0.25).*tanh(sqrt((9.81*0.25)./x)*4);
[A,B,C] = bisectt(f, -10, 10);
to do the calculation over -10 to +10

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by