Function Error Input Argument

1 Ansicht (letzte 30 Tage)
Robert  Flores
Robert Flores am 24 Jan. 2019
Beantwortet: Star Strider am 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 Kommentare
TADA
TADA am 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)
Robert  Flores
Robert Flores am 24 Jan. 2019
Thanks, this cleared up my issues.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by