I want to enter a mathematical function as an argument to a function

1 次查看(过去 30 天)
Hello, to be more precise: I want to call in my program a function that I have created and that allows to display the plot of a mathematical function, so I want to enter in argument of this function a mathematical expression that can be for example "sin(t) or 4*t+9". Knowing that the interval "t" is defined inside the function.
My problem is the following, when I launch the program it says to me that it does not know "t", logic since at the moment or the program reads it it is not yet defined.
How to solve this problem?
main program
plot_compare(sin(t) , 5 ,20);
secondary program
function plot_compare(f,N,M)
t = [];
S_N = [];
t = linspace(-pi ,pi ,M);
b = linspace (0,10,N);
S_N = sinesum(t,b);
plot(t,f,t,S_N);
end

采纳的回答

Stephen23
Stephen23 2022-9-27
编辑:Stephen23 2022-9-27
The simple approach using a function handle:
plot_compare(@sin , 5 ,20);
plot_compare(@(t) 4*t+9 , 5 ,20);
function plot_compare(fnh,N,M)
t = linspace(-pi ,pi ,M);
b = linspace (0,10,N);
%S_N = sinesum(t,b);
plot(t,fnh(t));
end
  1 个评论
Paul
Paul 2022-9-27
Thank you
I had found the beginning of a solution on the internet with the handle function but I understood where my mistake was, I must specify in the plot function that my function "f" must be plotted as a function of "t" so it's
plot(t,f(t))
and not
plot (t,f)
Thank you again

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by