MATLAB A problem in Matlab ,code doesn't work

  • Thread starter Thread starter Physics_rocks
  • Start date Start date
  • Tags Tags
    Matlab Work
AI Thread Summary
The discussion centers on a code snippet for calculating Simpson's Rule in MATLAB, highlighting an issue with the use of the `eval` function. The user is attempting to integrate functions like `sin` and `cos` but encounters an error stating "Not enough input arguments." This error arises because `eval(f)` is not correctly passing the necessary arguments to the functions. The conversation emphasizes the importance of understanding the `eval` function in MATLAB, which evaluates strings as MATLAB expressions. The user is encouraged to refer to the MATLAB documentation for clarification on `eval` and how to properly implement function evaluations within their code. The need for `n` to be even is also noted, as it is a requirement for Simpson's Rule.
Physics_rocks
Messages
12
Reaction score
0
Hi guys ,

I need to write a code in order to calculate Simpson's Rule.
Here is my code , what's wrong with it ?

PHP:
function [integral]=simp(f,a,b,n)
   if  mod(n,2)~=0
       warning('n must be even!')
   else
    h=(b-a)/n;
    integral=0;
    for i=1:1:n-1
        x=a+i*h;
        if (mod(i,2)==0)
            integral=integral+2*eval(f);
        else
            integral=integral+4*eval(f);
        end
    end
    x=a;
    integral=integral+eval(f);
    x=a+n*h;
    integral=integral+eval(f);
    integral=(h/3)*integral;
  end
Here is the link to the actual formula
"[URL


Thanks a lot :)
 
Last edited by a moderator:
Physics news on Phys.org
What is "eval(f)" supposed to do? What does the Matlab documentation have to say about the function eval?
 
D H said:
What is "eval(f)" supposed to do? What does the Matlab documentation have to say about the function eval?

for example , when I type in Matlab the following input :

simp('sin',0,pi/3,40)+simp('cos',0,pi/3,40)

sin suppose to be used , but it doesn't .

It says : "? Error using ==> sin
Not enough input arguments. "

"Error in ==> integral=integral+4*eval(f); "
 
Read the documentation. What does eval do?
 

Similar threads

Replies
8
Views
2K
Replies
2
Views
3K
Replies
4
Views
1K
Replies
4
Views
4K
Replies
10
Views
3K
Back
Top