Solving for Variables contained an interval

조회 수: 9 (최근 30일)
Joe
Joe 2023년 3월 11일
댓글: Joe 2023년 3월 11일
How do I solve the equation y = (sin(x) * (2* cos(x) - 1)) / (1 + 2 * cos(x)) only for x in the intervall [0 1], because if solved for all x there are infinte solutions. Thank you all for your help!

채택된 답변

Paul
Paul 2023년 3월 11일
Referring to the form of y given in this comment:
syms x real
y = sin(x)*(2*cos(x) - 1) / ((1 + 2*cos(x)) * (1 - cos(x)))
y = 
fplot(y,[0 1]) % don't know why the plot shows up below and not here?
Find the inverse function
fx = (finverse(y))
fx = 
Sub so that we get a function of the form where we input y and output x
syms yy real
fx(yy) = subs(fx,x,yy)
fx(yy) = 
Imaginary part is zero
imag(fx)
ans(yy) = 
0
So all we need is the real part
fx = real(simplify(fx,100))
fx(yy) = 
Check a value
fx(25)
ans = 
copyobj(gca,figure)
hold on
yline(25);
xline(double(fx(25)))
axis([0 0.1 0 50])
  댓글 수: 1
Joe
Joe 2023년 3월 11일
Thanks you gave me exactly what I need

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

추가 답변 (2개)

Askic V
Askic V 2023년 3월 11일
편집: Askic V 2023년 3월 11일
fun = @(x) (sin(x) .* (2.* cos(x) - 1)) ./ (1 + 2 .* cos(x)); % function
x0 = [0.1 2]; % initial interval
x = fzero(fun,x0)
x = 1.0472
t = linspace(0,2);
y = fun(t);
plot(t,y)
grid on
The function has value zero at x = 0, and the next one is at 1.0472. Therefore in the interval between 0 and 1 there is only a solution at 0.
  댓글 수: 4
Joe
Joe 2023년 3월 11일
편집: Joe 2023년 3월 11일
yes now i noticed the 2 y values for one x too. Since this is for a physics projekt I have a second equation I could use for which there is only one y value for one x in the interval [0 1]. It goes like this: y = sin(x)*(2*cos(x) - 1) / ((1 + 2*cos(x)) (1 - cos(x))). Also it doesn't matter that y vlaues are very small. It would also be possible to set y to an fixed value like 4 and slove for that. Thanks again for your help this would take me sooo much longer without your help
Walter Roberson
Walter Roberson 2023년 3월 11일
syms x y real
eqn = y == sin(x)*(2*cos(x) - 1) / ((1 + 2*cos(x)) * (1 - cos(x)))
eqn = 
sol = solve(eqn, x, 'returnconditions', true)
sol = struct with fields:
x: [3×1 sym] parameters: k conditions: [3×1 sym]
sx = simplify(sol.x, 'steps', 20)
sx = 
sol.conditions
ans = 
b1L = solve(sx(1) == 0, sol.conditions(1), 'returnconditions', true)
b1L = struct with fields:
k: [0×1 sym] y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
b1U = solve(sx(1) == 1, sol.conditions(1), 'returnconditions', true)
b1U = struct with fields:
k: (log((exp(3i)*1i + 1i)^2/(exp(3i) - 1)^2 + ((exp(3i)*1i + 1i)*2i)/(exp(3i) - 1) - 1)*1i - log((exp(3i)*1i + 1i)^2/(exp(3i) - 1)^2 + 1)*1i + 3)/(6*pi) y: (exp(3i)*1i + 1i)/(exp(3i) - 1) parameters: [1×0 sym] conditions: symtrue
sk = simplify(b1U.k, 'steps', 20)
sk = 
0
sy = simplify(b1U.y, 'steps', 20)
sy = 
vpa(sy)
ans = 
b2L = solve(sx(2) == 0, sol.conditions(2), 'returnconditions', true)
b2L = struct with fields:
k: [0×1 sym] y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
b2U = solve(sx(2) == 1, sol.conditions(2), 'returnconditions', true)
b2U = struct with fields:
k: [0×1 sym] y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
b3L = solve(sx(3) == 0, sol.conditions(3), 'returnconditions', true)
b3L = struct with fields:
k: [0×1 sym] y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
b3U = solve(sx(3) == 1, sol.conditions(3), 'returnconditions', true)
b3U = struct with fields:
k: [0×1 sym] y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
So out of the three analytic branches for the equation, only one of them can be probed for boundaries. The first of them has no y boundary at x == 0 because the equation goes to infinity. It does have a y boundary at x == 1 of roughly 0.0709

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


John D'Errico
John D'Errico 2023년 3월 11일
편집: John D'Errico 2023년 3월 11일
syms x
y = (sin(x) * (2* cos(x) - 1)) / (1 + 2 * cos(x));
xsol = solve(y == 0)
xsol = 
There are only three primary solutions.
xsol = solve(y == 0,'returnconditions',true)
xsol = struct with fields:
x: [3×1 sym] parameters: k conditions: [3×1 sym]
As you can see, now solve treturns a more complete result.
xsol.x
ans = 
xsol.conditions
ans = 
So, for integer k, the set of all solutions is one of those given in xsol.x, parameterized by the integer value of k.
That first positive solution is at pi/3, whhich falls just slightly outside of the interval [0,1]. So the only solution in that interval is 0 itself.
pi/3
ans = 1.0472
And that is exactly what @Askic V told you. Best to just use fzero.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by