multiple integration with non uniform spacing
Show older comments
I need help to find an area intergeral of the attached file(av_val_1) with 10 columns.
where 1st column = R , second column = C, Fifth column = F(R,C).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
R= avg_val_1(:,1);%first columsn = r axis
C = avg_val_1(:,2);%second column = c axis
F = avg_val_1(:,5);%fifth column = F(r,c)
I = trapz(C,trapz(R,F,2)); %area integeral of the total area. This line is wrong.
And
q = integral2(F(r,c),Rmin,Rmax,Cmin,Cmax) %area integeral of the particular area portion. This line is wrong. please see attached figure for reference
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Accepted Answer
More Answers (3)
MS
on 26 Apr 2020
Edited: Ameer Hamza
on 26 Apr 2020
13 Comments
Ameer Hamza
on 26 Apr 2020
You can use writematrix to save these three values.
for i=1:numel(avg_mat)
writematrix(avg_mat{i}, ['avg_val_' num2str(i)]);
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
F = avg_mat{i}(:,5);
r = linspace(min(R), max(R), 1000);
c = linspace(min(C), max(C), 1000);
[Rg, Cg] = meshgrid(r, c);
Fg = griddata(R, C, F, Rg, Cg);
result = trapz(c, trapz(r, Fg, 2));
writematrix([min(r) max(r) result], ['min_max_integral_val' num2str(i)])
end
Ameer Hamza
on 26 Apr 2020
To save in single file:
results = zeros(numl(avg_mat),1);
for i=1:numel(avg_mat)
writematrix(avg_mat{i}, ['avg_val_' num2str(i)]);
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
F = avg_mat{i}(:,5);
r = linspace(min(R), max(R), 1000);
c = linspace(min(C), max(C), 1000);
[Rg, Cg] = meshgrid(r, c);
Fg = griddata(R, C, F, Rg, Cg);
result(i) = trapz(c, trapz(r, Fg, 2));
end
writematrix(result, 'integral_val.txt')
I am not clear about the 2nd question.
Ameer Hamza
on 26 Apr 2020
You can use getrect(): https://www.mathworks.com/help/images/ref/getrect.html to manually specify the rectangular coordinate on an image. I am not sure how to handle the area integration for a different orientation.
Also, automating it does not seems to be an easy task. It will require some sophisticated image analysis skills.
MS
on 26 Apr 2020
Ameer Hamza
on 26 Apr 2020
Glad to be of help.
MS
on 26 Apr 2020
Edited: Ameer Hamza
on 26 Apr 2020
Ameer Hamza
on 26 Apr 2020
I think that xy1 and xy2 are written incorrect. Also mesh is not used correctly. Try this
for i=1:numel(avg_mat)
writematrix(avg_mat{i}, ['avg_val_' num2str(i)]);
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
F = avg_mat{i}(:,6);
rc1 = [min(R), min(C)];
rc2 = [max(R), max(C)];
t = linspace(0,1,1000)';
rcseg = (1-t)*rc1 + t*rc2;
r = linspace(min(R), max(R), 1000);
c = linspace(min(C), max(C), 1000);
[Rg, Cg] = meshgrid(r, c);
Fg = griddata(R, C, F, Rg, Cg);
fseg = interp2(Rg,Cg,Fg,rcseg(:,1),rcseg(:,2)); %%this line is wrong, The number of input coordinate arrays does not equal the number of dimensions (NDIMS) of these arrays%%%%
d = cumsum([0;sqrt(sum(diff(rcseg).^2,2))]);
line_integeral = trapz(d,fseg)
end
Ameer Hamza
on 26 Apr 2020
I am not sure about this issue. You can to see the specification of your dataset, that which column corresponds to which data. Also, I am not sure why two integrals should be equal.
Ameer Hamza
on 26 Apr 2020
Sorry, I don't have much expertise in multivariate calculus.
Isn't this just two independent integrals. You can do it like this.
trapz(r,u) + trapz(c,v)
Ameer Hamza
on 27 Apr 2020
For simple integrals, you don't need to use geiddata. You can do something like this
%%%%%%%%%%%%%%%%
for i=1:numel(avg_mat)
writematrix(avg_mat{i}, ['avg_val_' num2str(i)]);
R = avg_mat{i}(:,1);
C = avg_mat{i}(:,2);
u = avg_mat{i}(:,3);
v = avg_mat{i}(:,4);
line_integeral(i)= trapz(R,u) + trapz(C,v)
% fseg = interp2(Rg,Cg,Fg,rcseg(:,1),rcseg(:,2)); %%this line is wrong, The number of input coordinate arrays does not equal the number of dimensions (NDIMS) of these arrays%%%%
% d = cumsum([0;sqrt(sum(diff(rcseg).^2,2))]);
% line_integeral(i) = trapz(d,fseg)
end
8 Comments
Ameer Hamza
on 27 Apr 2020
You can set NaN values to 0.
u(isnan(u)) = 0;
v(isnan(v)) = 0;
MS
on 27 Apr 2020
MS
on 27 Apr 2020
Edited: Ameer Hamza
on 28 Apr 2020
Ameer Hamza
on 28 Apr 2020
The code logic is getting quite complicated to understand. Only you can understand what is happening at each line. I suggest you add breakpoints in your code and run it line by line. Then you can see which line is not working as expected.
MS
on 29 Apr 2020
Ameer Hamza
on 29 Apr 2020
I am glad that my comments were of some help in your project.
MS
on 29 Apr 2020
Ameer Hamza
on 29 Apr 2020
MS
on 29 Apr 2020
0 votes
Categories
Find more on Logical 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!