Integration containing multiple variable and plotting
Afficher commentaires plus anciens
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 commentaires
Dyuman Joshi
le 24 Avr 2022
Modifié(e) : Dyuman Joshi
le 24 Avr 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
le 24 Avr 2022
Star Strider
le 24 Avr 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
le 24 Avr 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
le 24 Avr 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
le 24 Avr 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
le 24 Avr 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
le 24 Avr 2022
‘Also, How do you check a function's efficiency? Just checking it's time to run a sample of code?’
Supriya Khatoniar
le 25 Avr 2022
Réponse acceptée
Plus de réponses (1)
Sulaymon Eshkabilov
le 24 Avr 2022
0 votes
Use integral3: https://www.mathworks.com/help/matlab/ref/integral3.html
1 commentaire
Supriya Khatoniar
le 24 Avr 2022
Modifié(e) : Supriya Khatoniar
le 24 Avr 2022
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
