Integration containing multiple variable and plotting
Show older comments
Can anybody help me to integrate & plot in the following format, i.e. contains multiple variables?:
z = 0:0.1:10;
a = -0.5:0.1:1;
x = 0:0.1:2;
y = 2*exp(-0.5*z) + x;
I = 0.2 ∫(0.5*(a-y)+0.3) dx
integration limit : 0 to x
plot (I,x);
10 Comments
Dyuman Joshi
on 24 Apr 2022
Edited: Dyuman Joshi
on 24 Apr 2022
What's the value of a? Is there any relation between z & x ? If no, it's a straight forward integration, treating z variable as constant.
Also - integration limits are a number, not an array
I STAND CORRECTED. YOU CAN USE ARRAY INPUT AS INTEGRATION LIMITS AS STAR STRIDER MENTIONED
Supriya Khatoniar
on 24 Apr 2022
Star Strider
on 24 Apr 2022
@Dyuman Joshi — You are actually correct, the integration limits must be scalars.
The arrayfun function contains an implied loop, so each value of ‘x’ is presented to integral in turn as the upper limit of integration, creating the family of curves.
Torsten
on 24 Apr 2022
I = 0.2 ∫(0.5*(a-y)+0.3) dx
together with
integration limit : 0 to x
makes no sense since the integration is with respect to x.
Dyuman Joshi
on 24 Apr 2022
@Star Strider Thank you for the clarification, I learnt something new today!
@Torsten, since 'a' and 'y' or not dependent of 'x', you can treat them as constant in integration and proceed further.
@Supriya Khatoniar, Do check Star Strider's answer, and mentioned if it is helpful or not.
y depends on x.
And it doesn't matter: Integration variable and upper limit as identical variable makes no sense.
What should
integral_{0}^{x} a dx
be ?
It's always
integral_{0}^{x} a dx'
or something similar.
Star Strider
on 24 Apr 2022
I agree, the ambiguities make this confusing. The original vectors are different lengths as well, so the original code as posted will obviously not work. I treated the ‘x’ in the integrand as separate from the vector ‘x’ and used the vector ‘x’ to define the upper limit of integration.
I have absolutely no idea what is actually intended here.
@Dyuman Joshi — My pleasure! Originally arrayfun was much slower than an equivalent loop, so I have not used it much. It is now much more efficient, and works here as a single-line substitute for a loop.
.
Dyuman Joshi
on 24 Apr 2022
I agree, the question is not framed clearly.
@Torsten, I overlooked the edit. My bad. What you mentioned is correct.
@Star Strider, wow thanks. New info. I got to know that arrayfun is quite slow as you mentioned, on MATLAB Answers only, but didn't know it has now become more efficient.
Also, How do you check a function's efficiency? Just checking it's time to run a sample of code?
Star Strider
on 24 Apr 2022
‘Also, How do you check a function's efficiency? Just checking it's time to run a sample of code?’
Supriya Khatoniar
on 25 Apr 2022
Accepted Answer
More Answers (1)
Sulaymon Eshkabilov
on 24 Apr 2022
0 votes
Use integral3: https://www.mathworks.com/help/matlab/ref/integral3.html
1 Comment
Supriya Khatoniar
on 24 Apr 2022
Edited: Supriya Khatoniar
on 24 Apr 2022
Categories
Find more on Numerical Integration and Differentiation 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!