Solving Trigonometric Function Equations with Time varying Parameters by MATLAB
显示 更早的评论
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])
采纳的回答
更多回答(1 个)
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






