Input formula to be used in a function

1 回表示 (過去 30 日間)
Lukas Underwood
Lukas Underwood 2019 年 11 月 6 日
回答済み: darova 2019 年 11 月 6 日
Hi all,
I have to write a function that can input different f(x) formulas and then use that formula to get solve for some variable in an output. I haven't been able to find anything that quite applies to what I'm trying to do. Any help is appreciated.
Ex.
function v = functionname(formula,a,b,c,d) %formula could be f(x) = x^2 + 2 or whatever
%I want to use the formula and other inputs to get an output
end

回答 (2 件)

Jeremy
Jeremy 2019 年 11 月 6 日
It sounds like you want
polyval
?
  1 件のコメント
Jeremy
Jeremy 2019 年 11 月 6 日
編集済み: Jeremy 2019 年 11 月 6 日
Without using polyval, something like this is working for me
function f = eval_eq(eq,a)
x = linspace(0,1,101);
f = eq(x,a);
plot(x,f)
end
and then call this with
y = @(x,a) a*x + 1;
f = eval_eq(y,2);
Seems a little out of the way to get it done though. I think just calling the anonymous function in the script is easier than passing it into a function

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


darova
darova 2019 年 11 月 6 日
Use function handle
function main
f = @(x) 2*x + 1; % function handle
func(f,2,3)
function res = func(f,a,b)
res = f(a)+b;
end
end

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by