where are the errors in my code? please help me!!
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Urgent Help ,please!
I would like to Solve an equation for a variable (y) while setting another variable (r) across a range and then substituting the solution into the upper and lower limits of an integral (q). Eventually, plotting the relationship between the intgeral (q) and the variable (r) with the range.
I know that there are multiple errors! but I can not figure out where are they!!
here is the code:
r=linspace(0,0.09);
syms y(r)
eqn= (cosh(10^10.*y./0.5)).^2 == 5./16.*r;
V1 = double(vpasolve(eqn,[y,r],[0 2]));
V2 = double(vpasolve(eqn,[y,r],[-2 0]));
fun=@(x)0.0018./((5./((cosh(10^10.*x./0.5)).^2)-r.*16).^0.5);
q = integral(fun,V2,V1)
plot(r,q)
채택된 답변
Torsten
2022년 5월 25일
syms r z
eqn = ((z+1/z)/2)^2 == 5/16*r;
S = solve(eqn,z);
y = log(S)*0.5/10^10
solves the equation
(cosh(10^10.*y./0.5)).^2 == 5./16.*r
for y.
Deduce the conditions on r that V1 and V2 in the subsequent integration are real-valued.
댓글 수: 20
Abdallah Qaswal
2022년 5월 26일
편집: Walter Roberson
2022년 5월 26일
Hi,
Thank you ver much for response!
Here what I wrote:
syms r z
eqn = ((z+1/z)/2)^2 == 5/16*r;
S =solve(eqn,z);
y = log(S)*0.5/10^10
y =

V1 = double(vpasolve(y,r,[0 0.09]));
Error using mupadengine/feval_internal
More equations than variables is only supported for polynomial systems.
More equations than variables is only supported for polynomial systems.
Error in sym/vpasolve (line 172)
sol = eng.feval_internal('symobj::vpasolve',eqns,vars,X0);
V2 = double(vpasolve(y,r,[0 0.09]));
fun=@(x)0.0018./((5./((cosh(10^10.*x./0.5)).^2)-r.*16).^0.5);
q = integral(fun,V2,V1)
plot(r,q)
Maybe, I do not understand what you meant. could you please write the full code? becuase my code still gives me error!
And did you find out which of the four solutions for y give a positive real number under the log(...) and what condition on r is necessary for that this is the case ?
I mean the expression sqrt(5*r-16) gives one necessary condition that your subsequent integration is senseful, namely r >= 16/5.
Thus
V2 = log(sqrt(5*r)/4 - sqrt(5*r-16)/4)/2e10
V1 = log(sqrt(5*r)/4 + sqrt(5*r-16)/4)/2e10
under the assumption
r >= 16/5
My problem is not with solving the equation but with how to insert the solution into the integral across the range of values of r. this is the error messege that I got:
Error using mupadengine/feval (line 187)
Symbolic parameters not supported in nonpolynomial equations.
Error in sym/vpasolve (line 172)
sol = eng.feval('symobj::vpasolve',eqns,vars,X0);
Error in edfdgf (line 4)
V1 = double(vpasolve(eqn,y,[0 2]));
Your problem is that you cannot integrate for the r-values you prescribe.
As said, r must be >= 16/5. You prescribe r as linspace(0:0.09) which is obviously < 16/5.
Or you must integrate over a complex line path from V2 to V1.
there should be something wrong, r in my physical case can not be more than 16/5. It should be within the range I put!!!
Well, MATLAB told you what V1 and V2 are depending on r - and these expressions are complex numbers if r < 16/5.
And that's logical since cosh(x) >= 1 for real x, thus the right-hand side of your equation
(cosh(10^10.*y./0.5)).^2 == 5./16.*r;
must be >=1, thus
5/16*r >= 1
thus
r >= 16/5
y = log(S)*0.5/10^10
If that is still how you are defining y then y is a vector of symbolic values. You cannot solve() something with respect to y when y is not a symbolic variable.
subs(eqn, r, y)
y = log(S)*0.5/10^10
is the symbolic vector-solution of the equation
eqn= (cosh(10^10.*y./0.5)).^2 == 5./16.*r;
got over the deviation to solve
eqn = ((z+1/z)/2)^2 == 5/16*r;
S = solve(eqn,z);
y = log(S)*0.5/10^10
first for z.
Or can MATLAB directly
syms r y
eqn = (cosh(10^10.*y./0.5)).^2 == 5./16.*r;
S = solve(eqn,y)
?
At least, both results should match.
ok, there is a minor mistake in my code, it is 5./(16.*r) not 5./16.*r! but still there is an error here in the following code:
syms r y
r=linspace(0,0.09);
eqn = (cosh(10^10.*y./0.5)).^2 == 5./(16.*r);
S1 = solve(eqn,y,[-5 0])
S2 = solve(eqn,y,[0 5])
fun=@(x)0.0018./((5./((cosh(10^10.*x./0.5)).^2)-16.*r).^0.5);
q = integral(@(x)fun(x),S2,S1)
plot(r,q)
this is the error messege
Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 54)
checkVariables(vars);
Error in solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in dsfdgfvbndresdfcv (line 4)
S1 = solve(eqn,y,[-5 0])
I already told you that this will not work.
This is code you can use:
% Prescribe solutions V2, V1 for (cosh(10^10.*y./0.5)).^2 == 5./16.*r
V2 = @(r) log(sqrt(5*r)/4 - sqrt(5*r-16)/4)/2e10
V1 = @(r) log(sqrt(5*r)/4 + sqrt(5*r-16)/4)/2e10
% Define function to be integrated
fun = @(x,r)0.0018./((5./((cosh(10^10.*x./0.5)).^2)-r.*16).^0.5);
% Define integral
q = @(r) integral(@(x)fun(x,r),V2(r),V1(r));
% Evaluate integral
r = 16/5:1/5:20;
Q = arrayfun(q,r)
% Plot result
plot(r,[real(Q);imag(Q)])
May I attach an image for the the problem written by hand so that you can visualize it better?
Is there something incorrect with the description in your question ?
You want to get the two real-valued solutions V1 and V2 with V1 > V2 of the equation
(cosh(10^10.*y./0.5)).^2 == 5./16.*r;
and use these solutions as lower and upper limit to evaluate the integral of the function
fun=@(x)0.0018./((5./((cosh(10^10.*x./0.5)).^2)-r.*16).^0.5);
for different values of r.
If this is correct, then the code I posted solves this problem.
Thank you! I think this will work!
It is much appreciated!
Thank you! It works !
Again! It is much appreciated!
If you choose
r = linspace(0,0.09)
you must know that V1 and V2 are complex numbers and that you integrate over a line in the complex domain, not over an interval in the reals.
But in case this doesn't matter for you, it's now also questionable which two of the four solutions of the equation
(cosh(10^10.*y./0.5)).^2 == 5./16.*r;
to take as lower and upper limits of the integral.
I assumed taking the real solutions under the assumption that r >= 16/5.
you may not notice my edit; it is not 5./16.*r rather it is 5./(16.*r), hence I got four new solutions and I picked the real ones:
y =
log(-(5^(1/2) + (5 - 16*r)^(1/2))/(4*r^(1/2)))/20000000000
log((5^(1/2) + (5 - 16*r)^(1/2))/(4*r^(1/2)))/20000000000
log(-(5^(1/2) - (5 - 16*r)^(1/2))/(4*r^(1/2)))/20000000000
log((5^(1/2) - (5 - 16*r)^(1/2))/(4*r^(1/2)))/20000000000
I picked the second and the fourth ones
Accordingly, the range (0 ,0.09) results in real plotting
Yes, the little things ...
추가 답변 (1개)
Walter Roberson
2022년 5월 26일
Your first solve() returns multiple roots.
Your vpasolve() is then trying to process all of the roots at the same time .
Remember that solve() and vpasolve() are trying to solve simultaneous equations. If you pass in four equations in one variable, it will not attempt to solve each of the equations independently: it will try to find a single value of the variable that satisfies all of the equations at the same time. (Imagine, for example,that you had a series of trig expressions in a single variable, then you might be trying to find the right period that solves all of the equations at once.)
If you want to solve each equation independently, then use arrayfun() to run solve.
arrayfun(@(Y) vpasolve(Y, r, [0 0.09]), Y, 'uniform', 0)
The 'uniform', 0 is there because vpasolve() might decide there are no solutions.
카테고리
도움말 센터 및 File Exchange에서 Equation Solving에 대해 자세히 알아보기
참고 항목
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)

