Function Error Input Argument

조회 수: 1 (최근 30일)
Robert  Flores
Robert Flores 2019년 1월 24일
답변: Star Strider 2019년 1월 24일
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
TADA
TADA 2019년 1월 24일
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 2019년 1월 24일
Thanks, this cleared up my issues.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2019년 1월 24일
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by