I am trying to use a script to run simpsons rule to numerically integrate a function, it keeps giving me an error.

1 view (last 30 days)

Answers (2)

Walter Roberson
Walter Roberson on 6 Jul 2017
You are trying to pass parameters to a script. You cannot do that. In order to be able to pass parameters to something, it has to be a function (or a class constructor.)
A script is a .m file whose first executable token is not either "function" or "classdef". You cannot pass parameters to them.
You need to add a "function" header line to your code.

James Tursa
James Tursa on 6 Jul 2017
Edited: James Tursa on 6 Jul 2017
A script is an m-file that has MATLAB statements in it only, without a function statement. E.g.,
File myfun1.m:
x = 2*a;
File myfun2.m:
function x = myfun2(a)
x = 2*a;
return
end
The first file, myfun1.m, is a script file. It simply executes MATLAB statements based on the variables in your workspace. The second file, myfun2.m, is a function file. It takes an input argument and returns an output.
Your simpsonsrule.m file is apparently a script file like myfun1.m, but you are trying to call it like a function that takes inputs and returns outputs. To do that, you need to add in the function statement and set the return variables.

Community Treasure Hunt

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

Start Hunting!