Trapezoidal Rule Involving Elliptical Integrals/Functions

조회 수: 2 (최근 30일)
Nicholas Davis
Nicholas Davis 2021년 3월 1일
댓글: Mathieu NOE 2021년 3월 11일
Hi. I'm trying to write a code to integrate a function via the trapezoidal rule. I can't seem to get my graph to produce the correct output. I have attached an image of the expected output. The function I am attempting to integrate is dphi = alpha/p. I am also unsure if I used the correct formula for the trapezoidal rule, so any clarity there would be appreciated. The first figure is exactly what I am looking for but the second figure cannot produce the correct result. I don't assume any fault in the math here as plotting phi versus x should be simple. I have attempted this issue before with Matlab's integral command but it still did not produce the correct plot.
clear all;
% Limits of Integration
a = 0; b = 1; n = 100;
h = (b-a)/n;
% Prerequisites
x = linspace(0,1,n);
m = 0.999999129426574;
J = 1;
L = 0.04;
[K,E] = ellipke(m);
r = zeros(1,numel(x));
dphi = zeros(1,numel(x));
trapphi = zeros(1,numel(x));
% Function
s = 1 + 8*J^2*L^2*E*K;
t = 8*J^2*L^2*K^2;
alpha = (1/sqrt(2)*L)*sqrt(s*(s-(1-m)*t)*(s-t));
for i = 1:n-1
u = 2*J*K*x(i);
[SN] = ellipj(u,m);
r(i) = s - t + t * m * SN^2;
dphi(i) = alpha/r(i); % Function to integrate
trapphi(i) = (sum(dphi) - (dphi(i)+dphi(i+1))/2)*h;
phi(i) = mod(trapphi,2*pi);
end
% Plotting
figure(1) % phi versus x
plot(x,phi,'-k')
xlim([0,1]);
ylim([0,1]);
figure(2)
plot(x,r,'-k') % r versus x
xlim([0,1]);
  댓글 수: 9
Nicholas Davis
Nicholas Davis 2021년 3월 10일
Hi all, I was able to get the plots from the paper perfectly with everyone's help. My professor originally advised I use the mod function to get my phi, but that was actually bad advice. In the new code I simply adjusted phi so instead of using the mod function, it was phi/(2*pi) and that gave me the correct range. For those interested, I was able to get all of the plots in figure 4 from the paper more or less perfectly. I need to develop a better newton's method code for finding the proper m values and thus getting better results, but I digress. I have attached the final code for all of you to marvel at what you helped create. Thank you everyone.
clear all
format long
% Constants ---------------------------------------------------------------
n = 1000;
x = linspace(0,1,n);
J = input('Enter J value: ');
L = 0.04;
r = zeros(1,numel(x));
dphi = zeros(1,numel(x));
% Trapz Command Integration -----------------------------------------------
if J == 1
for i = 1:n
m = 0.999999129426574;
[K,E] = ellipke(m);
u = 2*J*K*x(i);
[SN] = ellipj(u,m);
s = 1 + 8*J^2*L^2*E*K;
t = 8*J^2*L^2*K^2;
r(i) = s - t + t * m * SN^2;
alpha = (1/(sqrt(2)*L))*sqrt(s*(s-(1-m)*t)*(s-t));
dphi(i) = alpha/r(i);
end
trapzphi = cumtrapz(x,dphi);
phi = mod(trapzphi,2*pi);
elseif J == 2
for i = 1:n
m = 0.997999129426574; % 0.993524799088048;
[K,E] = ellipke(m);
u = 2*J*K*x(i);
[SN] = ellipj(u,m);
s = 1 + 8*J^2*L^2*E*K;
t = 8*J^2*L^2*K^2;
r(i) = s - t + t * m * SN^2;
alpha = (1/(sqrt(2)*L))*sqrt(s*(s-(1-m)*t)*(s-t));
ialpha = abs(alpha);
dphi(i) = ialpha/r(i);
end
trapzphi = cumtrapz(x,dphi);
phi = trapzphi/(2*pi); % mod(trapzphi,2*pi);
end
% Plotting ----------------------------------------------------------------
clf
figure(1) % r versus x
plot(x,r,'-k')
figure(2)
plot(x,phi,'-k') % phi versus x
Mathieu NOE
Mathieu NOE 2021년 3월 11일
Congrats to the team work !

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by