How to plot an anonymous function with multiple variables

13 ビュー (過去 30 日間)
Greg
Greg 2022 年 7 月 15 日
編集済み: Stephen23 2022 年 7 月 31 日
I need to plot a function that I have created as an anonymous function
I appreciate any help I can get!
Thanks!

採用された回答

Voss
Voss 2022 年 7 月 15 日
With the function as given, you can calculate its value for each a in a loop:
Func2 = @(a,b,c) 20*b^2-4*a^6+20+4*c;
a = 0:0.25:10;
b = -3;
c = 8;
vals = zeros(size(a));
for ii = 1:numel(vals)
vals(ii) = Func2(a(ii),b,c);
end
and plot:
plot(a,vals,'.-')
Or you could vectorize the function and calculate its values for all a at once:
Func2 = @(a,b,c) 20*b.^2-4*a.^6+20+4*c; % now any of a,b,c can be a vector (or all of them, if they are the same size)
a = 0:0.25:10;
b = -3;
c = 8;
vals = Func2(a,b,c);
and plot:
plot(a,vals,'.-')
  2 件のコメント
Greg
Greg 2022 年 7 月 15 日
Ah, I hadn’t even seen the vals function before. Thank you! What does the “.-“ do in the plot function?
Voss
Voss 2022 年 7 月 15 日
vals is a variable I made to store the values of Func2 at the given a, b, c. The line
vals = Func2(a,b,c);
evaluates the function Func2 at a, b, c, and stores the result in vals.
.- plots the line using dots as a data marker ( . ) and using a solid line ( - )

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by