Call specific expression index from a function carrying multiple expression

1 view (last 30 days)
I had defined a function
function y=func (x)
y=[x;x.^2];
Now I have to integrate each expression i.e. x and x^2 for different limits. Eg. quad('x',1,5) and quad('x^2',5,9)..How can we do this using the function as defined earlier?

Accepted Answer

Walter Roberson
Walter Roberson on 28 Aug 2015
Break it up in to two different integrations with different functions and add the two results together.
You can use helper functions such as
row1 = @(x) x(1,:);
row2 = @(x) x(2,:);
quad(@(x) row1(f(x)), 1, 5) + quad(@(x) row2(f(x)), 5, 9)
  3 Comments
Walter Roberson
Walter Roberson on 28 Aug 2015
row_n = @(x,n) x(n,:);
quad(@(x) row_n(f(x),1), 1, 5) + quad(@(x) row_n(f(x),2), 5, 9);
Or perhaps you would prefer
partsbounds = [1 5 9 10 15]; %break-points
fun_select = [1 2 2 1]; %which subresult for each range
total_integral = 0;
for K = 1 : length(fun_select)
total_integral = total_integral + quad(@(x) run_n(f(x), fun_select(K)), partsbounds(K), partsbounds(K+1));
end
... and I suspect that I've just done someone's homework assignment :(
Piyush Pallav
Piyush Pallav on 28 Aug 2015
Haha..Thank you..I hope next time I will ask the question after the homework assignment is over ;)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!