필터 지우기
필터 지우기

Getting error in this code.

조회 수: 1 (최근 30일)
AVINASH SAHU
AVINASH SAHU 2022년 6월 2일
댓글: Jan 2022년 6월 3일
betasqr=0.2;
gamma=0.3;
psi = 0.001;
mu = 1.0;
sbar = 50;
A = @(x) (4+sbar)-(sbar*betasqr*gamma).*(x.*(1-x)).^0.5;
B = @(x) (1-(betasqr)*((x.*(1-x)).^(0.5)));
E = @(x) ((6*(2+sbar))-((2*betasqr*sbar*gamma)*((x.*(1-x)).^(0.5))));
G = @(x) (12*psi*(1+sbar)) + A/B;
C = @(x) E/G;
IntEbyG = integral(C,0,1)
  댓글 수: 1
Jan
Jan 2022년 6월 2일
편집: Jan 2022년 6월 2일
A simpler version - note that sqrt(x) is much cheaper than x^0.5:
betasqr = 0.2;
gamma = 0.3;
psi = 0.001;
mu = 1.0;
sbar = 50;
A = @(x) 4 + sbar - sbar*betasqr*gamma .* sqrt(x .* (1-x));
B = @(x) 1 - betasqr * sqrt(x .* (1-x));
E = @(x) 12 + 6 * sbar - 2*betasqr*sbar*gamma * sqrt(x .* (1-x));
G = @(x) 12 * psi * (1 + sbar) + A/B; % ERROR
C = @(x) E/G; % ERROR
IntEbyG = integral(C,0,1)
Error using /
Arguments must be numeric, char, or logical.

Error in solution (line 9)
G = @(x) 12 * psi * (1 + sbar) + A/B;

Error in solution (line 10)
C = @(x) E(x)/G(x);

Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);

Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
I've displayed the error message. This is very useful, if you ask a question, because solving a problem is much easier than guessing, what the problem is. Whenever you mention an error in the forum, attach a copy of the complete message.

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

채택된 답변

Jan
Jan 2022년 6월 2일
A simpler version - note that sqrt(x) is much cheaper than x^0.5:
betasqr = 0.2;
gamma = 0.3;
psi = 0.001;
mu = 1.0;
sbar = 50;
A = @(x) 4 + sbar - sbar*betasqr*gamma .* sqrt(x .* (1-x));
B = @(x) 1 - betasqr * sqrt(x .* (1-x));
E = @(x) 12 + 6 * sbar - 2*betasqr*sbar*gamma * sqrt(x .* (1-x));
G = @(x) 12 * psi * (1 + sbar) + A(x) ./ B(x); % <-- bug fix
C = @(x) E(x) ./ G(x); % <-- bug fix
IntEbyG = integral(C,0,1)
IntEbyG = 5.3442
The error message tells you, that / does not work for function handles. Append the argument.
  댓글 수: 3
Image Analyst
Image Analyst 2022년 6월 2일
If Jan solved the problem, then please click the "Accept this Answer" link to award Jan his "Reputation points." Thanks in advance. 🙂
Jan
Jan 2022년 6월 3일
I have more reputation points than I need for my daily living. I'd prefer cookies: 🍪
For other readers it is useful to mark a question as solved by accepting an answer.

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

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by