Integration containing multiple variable and plotting

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 Commenti

Dyuman Joshi
Dyuman Joshi il 24 Apr 2022
Modificato: Dyuman Joshi il 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
@Dyuman Joshi Edited (d=a, and can append x in y's equation), now check. I just gave here a random equation to know the method if there's mutiple variable, then how can it be solved!
@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.
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.
@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.
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.
.
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?
Also, How do you check a function's efficiency? Just checking it's time to run a sample of code?
That is how I usually do it. The timeit approach is most accurate, however I usually just use tic and toc. and occasionally the profile function, depending on the information I am interested in.
@Dyuman Joshi @Star Strider @Torsten glad to see your fruitfull discussion. Thanks, I'll go through it. I wish to apply this kind of logic in some other codes & if I face some problem, I'll ask.

Accedi per commentare.

 Risposta accettata

To use vectors as arguments in the integrand, it is necessary to use integral and the 'ArrayValued',true name-value pair —
% z = 0:0.1:10;
% a = -0.5:0.1:1;
% x = 0:0.1:2;
N = 5; % Determines The Lengths Of The Vectors
z = linspace(0, 10, N);
a = linspace(-0.5, 1, N);
x = linspace(0, 2, N);
y = @(x) 2*exp(-0.5*z) + x;
I = arrayfun(@(x) 0.2 * integral(@(x) 0.5*(a-y(x))+0.3, 0, x, 'ArrayValued',1), x, 'Unif',0)';
Im = cell2mat(I);
figure
plot(x, Im)
legend(compose('x = %.2f',x), 'Location','best')
.

Più risposte (1)

Use integral3: https://www.mathworks.com/help/matlab/ref/integral3.html

Community Treasure Hunt

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

Start Hunting!

Translated by