Function Error Input Argument
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Robert Flores
le 24 Jan 2019
Réponse apportée : Star Strider
le 24 Jan 2019
Hello, I am trying to make a function to which I can call any arbitrary function and get an approximate value for the sum of my arbitrary function. I keep getting an error though whenever I try and use the function. If you can please help me, it will be much appriciated, thanks. Below is a copy of my code and in the attachments is a sceenshot of the error I am seeing.
function [Approximate_Value] = my_mean(fun,a,b,N)
% The function my mean should return an approximate value for the infinite
% sum of f(x) from a to b with respect to x. Where f(x) represents the
% function fun.
h = (b-a)/(N-1);
x = a+(1i-1)*h;
Approximate_Value = (h/2)*(fun(a)+fun(a+(N-1)*h)+sum(subs(h*f(x),x,1:(N-1))))
end
2 commentaires
TADA
le 24 Jan 2019
You are Invoking A Function Called f Here:
h*f(x)
This Functions apparently doesn't exist, Or It Exists In A Scope Thats Inaccessible To my_mean.
From Your Documentation im Assuming You Should Probably Change It To
h*fun(x)
Réponse acceptée
Star Strider
le 24 Jan 2019
First ‘f’ does not exist in your function because you do not define it in your code, or pass it as an argument.
Second, the subs function only works in the Symbolic Math Toolbox.
Try this instead (assuming that ‘f’ and ‘fun’ are the same):
Approximate_Value = (h/2)*(fun(a)+fun(a+(N-1)*h)+sum(h*fun(1:(N-1))));
Assuming that ‘a’, ‘h’, and ‘N’ are scalars, that should work.
0 commentaires
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!