Defining function for numerical intergration

9 ビュー (過去 30 日間)
Holly Miller
Holly Miller 2013 年 2 月 3 日
Hi All, I am new to Matlab and I am trying to define a function f=1/x and then compare the results of various numerical integration methods (quad, trapz, simpsons) from 0-1. I have entered my function in a new .m filed called f.m with the following: function y = f(x) y = 1 .* (x.^-1);
Then when I try to do the numerical integration methods I continually get errors relating to x, either it being undefined or saying the matrix must be square.
Any tips on how to define the variable x or to avoid these errors?
Thanks!

回答 (1 件)

the cyclist
the cyclist 2013 年 2 月 3 日
I had no problem using quad() like this:
Here is what my file f.m looks like:
function y = f(x)
y = 1.*(x.^-1);
Here is how I called it from the command line:
>> quad(@f,1,2)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by