Function Error Input Argument

1 visualización (últimos 30 días)
Robert  Flores
Robert Flores el 24 de En. de 2019
Respondida: Star Strider el 24 de En. de 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 comentarios
TADA
TADA el 24 de En. de 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 el 24 de En. de 2019
Thanks, this cleared up my issues.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 24 de En. de 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.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by