Calling Upon previous anonymous function results for use in a second anonymous function

1 回表示 (過去 30 日間)
I am plotting an anonymous function which produces values that I would like to use in plotting a second anonymous function. Is there anyway to store values of of the function obtained on the interval of the anonymous variable?
  2 件のコメント
Greg
Greg 2018 年 1 月 15 日
Can you post the relevant code to give us something to work with?
mht6
mht6 2018 年 1 月 15 日
編集済み: mht6 2018 年 1 月 15 日
P_x = @(x1) Psat2 + (Psat1 - Psat2)*x1;
fplot(P_x,[0,1])
Where Psat1 and Psat2 are constants. Ideally, I would then find a second set of y values (to plot another curve) using the P and x values that exist in the previous function. y = x1*Psat1/P is the equation. I know how to do this in a for loop but am wondering if there is any way to do this with an anonymous function. Thanks.

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

採用された回答

Star Strider
Star Strider 2018 年 1 月 15 日
編集済み: Star Strider 2018 年 1 月 15 日
You can use one anonymous function (or any other function) is a second function. You simply have to call it as a function in the second function.
Try this:
Psat1 = ...; % Provide Value
Psat2 = ...; % Provide Value
P_x = @(x1) Psat2 + (Psat1 - Psat2)*x1; % First Anonymous Function
y = @(x1) x1.*Psat1./P_x(x1); % Second Anonymous Function
--------------------
EDIT
This works for me:
Psat1 = rand;
Psat2 = rand;
P_x = @(x1) Psat2 + (Psat1 - Psat2).*x1; % First Anonymous Function
y1 = @(x1) x1.*Psat1./P_x(x1); % Second Anonymous Function
P_y = @(x1) Psat2 + (Psat1 - Psat2).*((y1(x1).*P_x(x1))./Psat1); % Third Anonymous Function
fplot(P_y,[0,1])
It is necessary to use Vectorization (link) in your functions when you combine them, so you do element-wise operations. (This will avoid warnings such as: ‘Function behaves unexpectedly on array inputs.’.)

その他の回答 (1 件)

Steven Lord
Steven Lord 2018 年 1 月 15 日
You want y to be a function of x1 that uses the existing P_x function?
% Sample values for Psat1 and Psat2
Psat1 = 1;
Psat2 = 2;
P_x = @(x1) Psat2 + (Psat1 - Psat2)*x1;
fplot(P_x,[0,1])
y = @(x1) x1*Psat1/P_x(x1)

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by