MATLAB Answers

numerical integration and solving for limit

19 views (last 30 days)
Benjamin on 13 Mar 2019
Commented: Star Strider on 18 Mar 2019
I have the following equation:
g(x) I have as a matrix in an array. It has an x and y column. So I want to take g(x) at a given poin, multiply by 1/x, then integrate, and find where the above statement is true. How can I write a script to solve for X_M for the above integral to be true?


Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 13 Mar 2019
You have told us nothing about ‘g(x)’. Assuming the integral of ‘g(x)’ is monotonically increasing at least until its integral is equal to 1 (if it’s periodic or has other pecularities, you will need another approach), try this:
x = linspace(1, 10); % ‘x’
y = rand(size(x)); % ‘g(x)’
vint = pi*sqrt(2)/3 * cumtrapz(x, y./x);
X_M = interp1(vint, x, 1);
Experiment to get the result you want.


Star Strider
Star Strider on 18 Mar 2019
For the cell in the code you posted, try this:
D = load('data.mat');
data =;
hold all
for i=1:17
plot(data{1, i}(:,2),data{1, i}(:,4));
C = data{1, i}(:,4)./data{1, i}(:,2);
vint = pi*sqrt(2)/3 * cumtrapz(data{1, i}(:,2), data{1, i}(:,4)./data{1, i}(:,2));
X_M(i) = interp1(vint, data{1, i}(:,2), 1);
Y_M(i) = interp1(data{1, i}(:,2), data{1, i}(:,4), X_M(i));
plot(X_M(i), Y_M(i), '+k')
hold off
xlim([1 5])
This uses essentially the same idea (an interp1 call) to calculate the y-value (that I chose to call ‘Y_M’ for consistency) associated with it, and plots it as a black ‘+’. This also saves the values of ‘Y_M’ as a vector for subsequent use. The xlim call eliminates the flat part of the plot in order to make thed plotted ’+’ markers more visible. (I apologise for the delay — I had to go back and remember what we were doing.)
Benjamin on 18 Mar 2019
wow, thanks! works perfectly! I'll have to dig into this to see what you did. I may ask a question later if I can't figure it out. you have been super helpful!
Star Strider
Star Strider on 18 Mar 2019
As always, my pleasure! Thank you!
You can always ask a question! (With luck, I will always have an answer.)
The ‘Y_M’ calculation takes the known independent (‘data{1, i}(:,2)’) and dependent (‘data{1, i}(:,4)’) vector values and uses the newly-derived value for ‘X_M(i)’ to calculate (interpolate) the value for ‘Y_M(i)’. The only other additions were to define the figure object and the hold call before the loop, and plot the ‘(X_M(i),Y_M(i))’ values within the loop. The later xlim call simply ‘zooms’ the x-axis slightly to make the plotted ‘+’ markers more visible.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by