A problem in Matlab ,code doesn't work

  • MATLAB
  • Thread starter Physics_rocks
  • Start date
  • Tags
    Matlab Work
In summary, the conversation revolves around a code for calculating Simpson's Rule. The code is being checked for errors and the function eval is being discussed. The person is asking for help in understanding what the eval function does and how to use it correctly in their code. They also mention an error they encountered while using the sin function. The conversation ends with a suggestion to read the documentation for further understanding of the eval function.
  • #1
Physics_rocks
12
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
  • #2
What is "eval(f)" supposed to do? What does the Matlab documentation have to say about the function eval?
 
  • #3
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); "
 
  • #4
Read the documentation. What does eval do?
 

1. Why is my code not running in Matlab?

There could be several reasons why your code is not running. It could be due to syntax errors, missing variables or functions, or incorrect data types. Make sure to check for any errors or warnings in the command window and review your code carefully.

2. How do I debug my code in Matlab?

To debug your code in Matlab, you can use the built-in debugger by setting breakpoints and stepping through your code line by line. You can also use the "dbstop" command to stop your code at a specific line and inspect variables. Additionally, using the "disp" function to print out values of variables at different points in your code can help identify where the error is occurring.

3. I keep getting errors when trying to run my code in Matlab, what should I do?

If you keep getting errors when trying to run your code, it could be due to a variety of reasons. Make sure to carefully read and understand the error message and check for any syntax errors. You can also try running your code in smaller sections to identify where the error is occurring. Additionally, seeking help from online forums or consulting with colleagues can also provide helpful insights.

4. How can I optimize my code in Matlab?

To optimize your code in Matlab, you can use vectorization, which involves using array operations instead of loops. This can significantly improve the performance of your code. You can also use the "profile" tool to identify bottlenecks in your code and improve its efficiency. Additionally, accessing Matlab's documentation and tutorials can help you learn more about efficient coding practices.

5. Can I run code written in other programming languages in Matlab?

Yes, you can run code written in other programming languages in Matlab by using the "mex" function. This function allows you to call functions written in C, C++, or Fortran from within Matlab. However, you will need to have a compatible compiler installed on your computer for this to work.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
Back
Top