0

I want to create an algortihm in MATLAB which calculates a specific function and then takes integral of it to get a specific value. The dimensions of the "result" is based on the "size" paramater in the beginning of the code. For example, if size = 100, result will be a 100x100 matrix. Because of the high dimension size and useage of integral() function, runtime gets very long. I think, it is mostly because of the runtime of integral() function. Is there a way to optimize the runtime of this algortihm. Complexity and memory usage is not a problem, only improvements in runtime are needed.

size = 100;

y = zeros(2,size);
y(1,:) = 10*(randn(1,size) + 1i*randn(1,size));
y(2,:) = randn(1,size) + 1i*randn(1,size);

result = zeros(size);

for j=1:size
    for i=1:size
        fun = @(x) sin(x)*(y(1,i)-y(1,j)) + cos(x)*(y(2,i)-y(2,j));
        result(i,j) = integral(fun,0,100);
    end
end

1 Answer 1

1

About 36% of the time used by integral is for handling the optional input arguments, which you are not using, so you could copy the integral function and rename it to something like myintegral.m. You could then edit the new function to eliminate this line of code:

opstruct = integralParseArgs(varargin{:});

If you set a breakpoint after the line above, you can get the contents of opstruct and hard code it into your new function so that you save the time parsing the arguments. You could save even more time by making opstruct a persistent variable.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't understand it. Should I remove the function from the for loop and define as a function? Also where does opstruct = integralParseArgs(varargin{:}); goes? Can you explain it in more detail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.