MATLAB: Does it keep saying that the output variable I is not assigned? It also says there are not enough input arguements for N = length(fx)

function errorinput arguementsintegrationoutputoutput errorsim83

function [I] = simp38int( h , fx )
% this function takes data sampled at evenly spaced locations as inputs and
%returns an approximation of the integral over the sample range based on
%Simpson?s multiple-application 3/8 rule
% Inputs:
% h : (scalar) step size seperating adjacent sampling locations
% fx: (N-element vector) function data taken at uniform locations sperated
% by a distance h
% Outputs
% I: approximation for the integral of fx for the sampled range
%Step one: check for error
N = length(fx); %getting length of fx for error check
%checking that h is a scalar
if isscalar(h) == 0
error('h is not a scalar')
end
%checking that fx is a vector then checking that it is an odd vector
if isvector(fx)==0
error('fx is not a vector')
%checking that h and fx both contain all numerical values
if isnumeric(h) == 0
error('h is not numerical')
elseif any(isnumeric(fx(:))) ==0
error('fx contains non numerical values')
end
sum3 =0
sum2 = 0
%implementing solving algorithm
for i = 2:3:N-1
sum3 = sum3 + 3*(fx(i)+fx(i+1))
end
for i = 4:3:N-2
z2= sum2+2*fx(i)
end
I = 3*h/8*(fx(1)+sum3+sum2+fx(N))
end

Best Answer

You have
if isvector(fx)==0
error('fx is not a vector')
followed by the rest of your code, with no end or else . None of that code can be reached, because if the condition is true then you would error() out, and if the condition was false then the if would skip everything inside body of the if. This is a reason why it is recommended that you highly all of your code and click on the "smart indent" tool, so you can see where MATLAB thinks your nesting ends, which might be different than what you have in mind.
Also, you cannot just click Run or use a menu entry to execute the code: you need to go down to the command line and give the command to run the file, such as
h = 0.000173;
fx = [-12, 85.12329, 46.8251, 57.3430, 73.23429];
simp38int( h , fx )
simp38int( h , fx )
Related Question