简体   繁体   中英

Integral piecewise function matlab

I am trying to integrate a function F which is defined as:

function F        
    x = -3:0.1:3;
    F = zeros(1, length(x));
    for i = 1:length(x)
        if (1.4<= x(i)) && (x(i) <= 1.6)
            F(i) = x(i).^2;
        else
            F(i) = 2;
        end
    end 
end

but the integral function gives me an error saying that there are too many arguments. I think the problem that the function is defined as a points?

The problem with your function, is that integral has no way to pass the arguments you supply to your function F . The function doesn't know that it can just pull certain elements from the vector you created. If you rewrite your function such that for an input (or x value), the output of F is returned, then integral will work as you need given two values to integrate between.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM