Definite Integrals in for-loop
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I'm trying to setup up this loop. But before I do that I need to declare the functions:
n_grid = 20; % Size of rid
Ms = [];
Js = [];
Ns = [];
Es = [];
J = linspace(0,1,1000);
kappa = 2.*sinh(2*J)./(cosh(2*J)).^2;
kappam = 2*(tanh(2*J)).^2-1;
z = exp(-2*J);
K1 = int(sqrt(1- kappam.^2 * sin(phi).^2), phi= 0.. pi/2)
% Calculating Energy & Magnetisation as a function of interaction strength
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1(kappam));
An error for K1 shows as follows:
Undefined function 'K1' for input arguments of type 'char'.
Error in ising (line 12)
K1 := int(sqrt(1- kappam.^2 * sin(phi).^2), phi= 0.. pi/2)
What's wrong with my syntax?
채택된 답변
Andrei Bobrov
2016년 9월 9일
편집: Andrei Bobrov
2016년 9월 9일
Please try code below
J = linspace(0,1,1000);
kappam = 2*(tanh(2*J)).^2-1;
f = @(phi)sqrt(1- kappam.^2 * sin(phi).^2);
K1 = integral(f,0,pi/2,'ArrayValued',true);
댓글 수: 11
T
2016년 9월 9일
Hi Andrei, thanks for your help. I receive the following error:
Subscript indices must either be real positive integers or logicals.
Subscript indices must either be real positive integers or logicals.
Error in ising (line 14)
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1(kappam));
Andrei Bobrov
2016년 9월 9일
What is it K1(kappam), may by just K1?
T
2016년 9월 10일
편집: Walter Roberson
2016년 9월 11일
K1 is an integral from page 3 (2.14)
Andrei Bobrov
2016년 9월 11일
편집: Andrei Bobrov
2016년 9월 11일
Hi T!
two variants:
first:
f = @(phi)1./sqrt(1- kappam.^2 * sin(phi).^2);
K1 = integral(f,0,pi/2,'ArrayValued',true);
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1);
second:
f = @(k,phi)1./sqrt(1- k.^2 * sin(phi).^2);
K1 = @(k)integral(@(phi)f(k,phi),0,pi/2,'ArrayValued',true);
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1(kappam));
T
2016년 9월 11일
편집: Walter Roberson
2016년 9월 11일
In using the second part:
Error using integralParseArgs (line 12)
Argument 'Ar' did not match any valid parameter of the parser.
Error in integral (line 88)
opstruct = integralParseArgs(varargin{:});
Error in @(k)integral(@(phi)f(k,phi),0,pi/2,'Ar',1)
Error in ising (line 14)
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1(kappam));
Nevertheless, I think 'k' should actually be kappa as defined in 2.12 on page 3 and in the code:
J = linspace(0,1,1000);
kappa = 2.*sinh(2*J)./(cosh(2*J)).^2;
Andrei Bobrov
2016년 9월 11일
편집: Andrei Bobrov
2016년 9월 11일
corrected
first:
f = @(phi)1./sqrt(1- kappa.^2 * sin(phi).^2);
K1 = integral(f,0,pi/2,'ArrayValued',true);
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1);
second:
f = @(k,phi)1./sqrt(1- k.^2 * sin(phi).^2);
K1 = @(k)integral(@(phi)f(k,phi),0,pi/2,'ArrayValued',true);
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1(kappa));
T
2016년 9월 13일
That works:
n_grid = 20; % Size of rid
Ms = [];
Js = [];
Ns = [];
Es = [];
J = linspace(0,1,1000);
kappa = 2.*sinh(2*J)./(cosh(2*J)).^2;
kappam = 2*(tanh(2*J)).^2-1;
z = exp(-2*J);
f = @(kappa,phi)1./sqrt(1- kappa.^2 * sin(phi).^2);
K1 = @(k)integral(@(phi)f(kappa,phi),0,pi/2,'ArrayValued',true);
% Calculating Energy & Magnetisation as a function of interaction strength
E = -J.*(coth(2*J)).*(1+(2/pi)*kappam.*K1(kappam));
M1 = ((1+z.^2).^(1/4).*(1-6*z.^2+z.^4).^(1/8))./(1-z.^2).^(1/2);
M2 = -((1+z.^2).^(1/4).*(1-6*z.^2+z.^4).^(1/8))./(1-z.^2).^(1/2);
M1(1,1:441) = 0; %When J < J_c, M = 0
M1(1,1:441) = 0; %When J < J_c, M = 0
for i = 1:12000
%while (1),
% Choose a random value between 0 and 1 for the interaction strength
J = rand()+1e-10;
% Perform a simulation
[M, N, E] = ising2(n_grid, J);
% Records the results
Ms = [Ms M/(n_grid^2)];
Es = [Es E/(n_grid^2)];
Ns = [Ns N];
Js = [Js J];
i = i+1;
end
Undefined function 'ising2' for input arguments of type 'double'.
Error in ising (line 27) [M, N, E] = ising2(n_grid, J);
If you look on page 12 of that PDF you will see that even they have an error message about a missing ising2 function or variable!
Your file that you have named "ising.m" should instead be named "montecarlo.m" . You should be taking the ising.m from that pdf and storing that as ising.m . And then you should create
function [M, num, E] = ising2(N, J)
[M, num, E] = ising(N, J);
and store that as ising2.m
T
2016년 9월 22일
편집: Walter Roberson
2016년 9월 22일
Done. Three errors arise:
Error: File: ising.m Line: 12 Column: 1
Function definitions are not permitted in this context.
Error in ising2 (line 2)
[M, num, E] = ising(N, J);
Error in montecarlo (line 27)
[M, N, E] = ising2(n_grid, J);
Were you able to reproduce the plots?
Walter Roberson
2016년 9월 22일
I had no problems with the software once I had done what I indicated above needed to be done.
You might notice that the code in ising.m is spaced oddly. That is because I copied it directly from the pdf and did not bother to change the spacing.
T
2016년 9월 22일
Strange. Those files work thanks.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
