Solving Trigonometric Function Equations with Time varying Parameters by MATLAB
조회 수: 7 (최근 30일)
이전 댓글 표시
Hello, my goal is to solve the equations about cos (x) and sin (x), plot the solution results into a curve, and the Lac in the equation will change with time.
I tried to use fsolve to solve the equation, but the result is only a fixed value, not a value that changes with time in theory. Here is my code. I look forward to receiving your guidance
%The function I created is as follows
function q=myfun(p)
x=p;
t=0:0.001:5;
Lac=0.91+0.01*sin(t);
q=2 * ( 0.0822*cos(x) - 0.2838*sin(x)) + Lac.^2 - 0.9209;
end
%Here is the command that I call the function
[x]=fsolve(@myfun,[0])
댓글 수: 0
채택된 답변
Davide Masiello
2022년 10월 10일
편집: Davide Masiello
2022년 10월 10일
You were almost there
t = (0:0.001:5)';
x = fsolve(@(x)myfun(x,t),zeros(size(t)));
plot(t,x)
xlabel('t')
ylabel('x')
function q = myfun(x,t)
Lac = 0.91+0.01*sin(t);
q = 2*(0.0822*cos(x)-0.2838*sin(x))+Lac.^2-0.9209;
end
댓글 수: 5
Davide Masiello
2022년 10월 10일
Well then, instead of passing t to the function and defining Lac(t), you just pass it the array of values like below
Lac = 0.91+0.01*sin((0:0.001:5)') % Lac is now an array of doubles
x = fsolve(@(x)myfun(x,Lac),zeros(size(Lac)));
plot((0:0.001:5)',x)
xlabel('t')
ylabel('x')
function q = myfun(x,Lac)
q = 2*(0.0822*cos(x)-0.2838*sin(x))+Lac.^2-0.9209;
end
추가 답변 (1개)
John D'Errico
2022년 10월 10일
편집: John D'Errico
2022년 10월 10일
Way better than using fsolve is to compute the analytical solution. That is, if t is the time varying parameter, then we have
syms t x
Lac=0.91+0.01*sin(t);
q = 2 * ( 0.0822*cos(x) - 0.2838*sin(x)) + Lac.^2 - 0.9209;
Now, we wish to solve q==0, for x, but as a function of t.
xsol = solve(q == 0,x,'returnconditions',true)
So there are infinitely many solutions, valu=id for any integer value of k.
xsol.x
That may look a bit messy, but we can first take only the primary solution, where k == 0. Agaoin, add any integer multiple of 2*pi to these solutions.
syms k
xprimary = subs(xsol.x,k,0)
Note there appear to be complex terms in this. We might want to plot the solutions, looking to see if and where imaginary terms enter in. When we do so, we will see the imaginary terms are essentially always zero. So they ended up canceling out always.
fplot(imag(xprimary(1)),[0,5],'b')
fplot(real(xprimary(1)),[0,5],'b')
hold on
fplot(real(xprimary(2)),[0,5],'r')
ylim([-3,1])
grid on
xlabel t
ylabel x(t)
These solutions are in terms of radians. If you want degrees, multiply by 180/pi. but since you used sin and cos in your equaritnis, I can only assume you want to see it in terms of radians.
Finally, IF you want a functional form you can call, then just use matlabFunction. Thus...
x1_t = matlabFunction(real(xprimary(1)));
x2_t = matlabFunction(real(xprimary(2)));
The positive solution was the second one. As you see, we can evaluate it directly for ANY value of t.
x2_t(3)
Thus 0.1286 radians.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!