A problem in Matlab ,code doesn't work

  • Context: MATLAB 
  • Thread starter Thread starter Physics_rocks
  • Start date Start date
  • Tags Tags
    Matlab Work
Click For Summary

Discussion Overview

The discussion revolves around a problem with a Matlab code implementation of Simpson's Rule for numerical integration. Participants are examining the code's functionality and identifying potential issues related to the use of the eval function.

Discussion Character

  • Technical explanation, Debate/contested, Homework-related

Main Points Raised

  • One participant presents a Matlab function intended to calculate Simpson's Rule and asks for help identifying errors.
  • Another participant questions the purpose of the "eval(f)" function within the code and prompts others to refer to the Matlab documentation for clarification.
  • A subsequent participant provides an example input to the function and reports an error message related to the use of the sin function, indicating that it does not receive the expected input arguments.
  • Another participant reiterates the suggestion to consult the documentation regarding the eval function.

Areas of Agreement / Disagreement

There is no consensus on the specific issue with the code, as participants are exploring different aspects of the eval function and its implications in the context of the provided code.

Contextual Notes

Participants have not resolved the underlying issues with the code, and there may be assumptions about the input format and function behavior that are not explicitly stated.

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 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K