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)
采纳的回答
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 个评论
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 个)
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)

