1

This is my matlab code , I got Not enough input argument error in line 2 and i don't know how to fix it. Anyhelp ? Thanks in advance .

function [] = Integr1( F,a,b )
i = ((b - a)/500);
x = a;k = 0; n = 0;
while x <= b
    F1 = F(x);
    x = x + i;
    F2 = F(x);
   m = ((F1+F2)*i)/2;
    k = k +m;
end
k
x = a; e = 0; o = 0;
while x <= (b - 2*i)
    x = x + i;
    e = e + F(x);
    x = x + i;
    o = o + F(x);
end
n = (i/3)*(F(a) + F(b) + 2*o + 4*e)
3
  • It seems to be working for me. Can you add more info please? How are you defining F? and how are you calling this function from the command line in order to test it? Commented Mar 10, 2015 at 0:00
  • The error message tells you what the problem is. You don't have enough input arguments. When you use this function you have to give it 3 inputs. Commented Mar 10, 2015 at 0:30
  • would you please tell me exactly the inputs you used when calling the function in the command window and how did you call it exactly. Sorry i am new to matlab and thank you very much for your answer. Commented Mar 10, 2015 at 0:34

1 Answer 1

1

This code performs integration by the trapezoidal rule. The last line of code gave it away. Please do not just push the Play button in your MATLAB editor. Don't even think about it, and ignore that it's there. Instead, go into your Command Prompt, and you need to define the inputs that go into this function. These inputs are:

  • F: A function you want to integrate:
  • a: The starting x point
  • b: The ending x point

BTW, your function will not do anything once you run it. You probably want to return the integral result, and so you need to modify the first line of your code to this:

function n = Integr1( F,a,b )

The last line of code assigns n to be the area under the curve, and that's what you want to return.

Now, let's define your parameters. A simple example for F is a linear function... something like:

F = @(x) 2*x + 3;

This defines a function y = 2*x + 3. Next define the starting and ending points:

a = 1; b = 4;

I made them 1 and 4 respectively. Now you can call the code:

out = Integr1(F, a, b);

out should contain the integral of y = 2*x + 3 from x = 1 to x = 4.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.