multiple integration with non uniform spacing
이전 댓글 표시
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
채택된 답변
추가 답변 (3개)
MS
2020년 4월 26일
편집: Ameer Hamza
2020년 4월 26일
댓글 수: 13
Ameer Hamza
2020년 4월 26일
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
2020년 4월 26일
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
2020년 4월 26일
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
2020년 4월 26일
Ameer Hamza
2020년 4월 26일
Glad to be of help.
MS
2020년 4월 26일
편집: Ameer Hamza
2020년 4월 26일
Ameer Hamza
2020년 4월 26일
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
2020년 4월 26일
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
2020년 4월 26일
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
2020년 4월 27일
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
Ameer Hamza
2020년 4월 27일
You can set NaN values to 0.
u(isnan(u)) = 0;
v(isnan(v)) = 0;
MS
2020년 4월 27일
MS
2020년 4월 27일
편집: Ameer Hamza
2020년 4월 28일
Ameer Hamza
2020년 4월 28일
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
2020년 4월 29일
Ameer Hamza
2020년 4월 29일
I am glad that my comments were of some help in your project.
MS
2020년 4월 29일
Ameer Hamza
2020년 4월 29일
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!