Solve trigonometrics equations in Matlab - matlab

I'm trying to solve for t trigonometric equations in Matlab, as i.e. 7*cos(t) + 5*sin(t) = 0. I would solve it as: sin(t)/cos(t) = -7/5 and I would find it as arctan(-7/5) = -0.9505.
I have tried to do it on matlab using solve function:
syms t
theta = solve(7*cos(t) + 5*sin(t)==0, t);
disp(theta);
But I get -(log(- 12/37 - (35*i)/37)*i)/2 instead of -0.9505. Could someone explain me why I get this answer from solve and how to get -0.9505?

The expression is the exact result, expressed symbolically (due to the use of syms).
To make Matlab display the result in the format your looking for use double(theta)
which should give you:
double(theta)
ans =
-0.9505

Related

How to use fsolve() with det()

I want to solve det(A)=0 for a large matrix A with each element a function of w.
One way to solve these (simple) problems is using a symbolic approach, e.g.:
A = sym('[w, 1; 2, 4*w^2 + 2]');
answer = solve(det(A),'w');
However, I want solve a much larger problem where the equation of each element is defined as a function handle (e.g. A4 = #(w) 4*w^2 + 2;), and may need to be solved numerically with fsolve().
The problem is that I cannot directly put the function handles in matrix A - they need to be put in a cell array, but then solve(det(A)) is incompatible with cell arrays and returns "Undefined function 'det' for input arguments of type 'cell'."
How do I solve the problem?
I have tried to following approach using you minimal example.
% Defining all functions as cells:
f{1,1} = #(w)w;
f{1,2} = #(w)1;
f{2,1} = #(w)2;
f{2,2} = #(w)4*w.^2+2;
% Function to solve
fsol = #(w)det(cellfun(#(x)x(w),f));
% Using fsolve
fsolve(fsol,0)
The result is
0.5898
which is equal to the (real) solution using symbolic math.

Using Interpolation in Matlab to return a function

I've got a silly problem, I'm looking to take a few data points, fit a polynomial function through it and then differentiate that function to get that particular functions optimal point. As such I have done some reading online and I've used the Matlab 'spline' function. Here is some code:
a = [50; 100; 150;200;250;300;350]
b = [56;23;22;18;14;15;21]
y = spline(a,b)
But when I used diff(y) I get the following error:
??? Error using ==> diff
Function 'diff' is not supported for class 'struct'.
I'm not too familiar with Matlab, so any help would really be appreciated
As per comments:
y = polyfit(a,b,2)
syms x
g = y(1)*x^2 + y(2)*x + y(3)
diff(g)
and you get the derivative of the function g. Much thanks to the guys in the comment section!

How do I solve a function for x in Matlab?

I have this function defined:
% Enter the data that was recorded into two vectors, mass and period
mass = 0 : 200 : 1200;
period = [0.404841 0.444772 0.486921 0.522002 0.558513 0.589238 0.622942];
% Calculate a line of best fit for the data using polyfit()
p = polyfit(mass, period, 1);
fit=#(x) p(1).*x + p(2);
Now I want to solve f(x) = .440086, but can't find a way to do this. I know I could easily work it out by hand, but I want to know how to do this in the future.
If you want to solve a linear equation such as A*x+B=0, you can easily solve in MATLAB as follows:
p=[0.2 0.5];
constValue=0.440086;
A=p(1);
B=constValue-p(2);
soln=A\B;
If you want to solve a non-linear system of equations, you can use fsolve as follows (Here I am showing how to use it to solve above linear equation):
myFunSO=#(x)0.2*x+0.5-0.440086; %here you are solving f(x)-0.440086=0
x=fsolve(myFunSO,0.5) %0.5 is the initial guess.
Both methods should give you the same solution.

Solving nonlinear minimization equations symbolically in matlab

I have a large underdetermined equation system for which I search an unique solution in respect of any given constraints. I simplified my problem into the following one:
x²-4=0,
y²-9=0,
x*y=myMin,
x+y=myMin.
What is the best way to implement this in Matlab symbolically, so that it returns
x=2
y=-3
I'm searching something like
syms x y
S=solve(...
x²-4==0,...
y²-9==0,...
x*y==myMin,...
x+y==myMin);
I do not know how specify the min as a function command to solve. But here's an approach that solves the equations and then post-processes the result according to your constraints:
syms x y
S=solve(x^2-4==0,y^2-9==0);
[~,idx] = min(double(S.x .* S.y)+double(S.x + S.y));
X = double(S.x(idx))
Y = double(S.y(idx))
This gives:
X =
2
Y =
-3
The symbolic results have to be converted using the double command to allow processing with the min function.
The problem you seem to run into is that there is no solution, not even matlab can deal with that.
Try it like this:
myMin = -6;
syms x y
S=solve(...
x²-4==0,...
y²-9==0,...
x*y==myMin,...
x+y==myMin + 5); %Note the +5 to make it feasible
Cannot try myself, but a quick calculation tells me that this one is at least solvable.

Using fminsearch to solve an equation

(vgb-phy_s)^2=G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))
where
x=phy_s/phy_t
phy_t=0.0288; % phy_t=k*T/q; (k=1.3806503*10^-23, T=300 K, q=1.6*10^-19)
phy_b=0.5267; % phy_b=phy_t*ln(Na/ni)
G=(sqrt(2*q*es*Na)/cox);
Here I need to plot phy_s for different values of vgb.
I tried many ways but since I'm new to matlab I'm on my learning process, I'm not able to find a proper solution.
Few people suggested me to use fminsearch but its quite confusing and I'm getting lot of errors.
fminsearch is a function for finding a minimum of a function, not for finding a solution of an equation. Further, here you don't have one equation but an equation group of at least 5 equations. You can use solve to solve equations and equation groups. However, an equation group of the following equations 1-5 does not have explicit solution. Another issue is that the constant values you propose seem to be imprecise values, and if you have more than one rounded or otherwise imprecise value, you can not find a solution even if the equation group was solvable (however, this equation group does not have [an explicit] solution)).
So, I'll show the steps to solve this but there seems to be something wrong with this equation group, even if the [probably imprecise] constant definitions (phy_t=0.0288; phy_t=k*T/q; (k=1.3806503*10^-23; T=300; q=1.6*10^-19; phy_b=0.5267;`) were left out.
Equations (without constant definitions):
1. (vgb-phy_s)^2 = G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))
2. x = phy_s/phy_t
3. phy_t = k*T/q
4. phy_b=phy_t*ln(Na/ni)
5. G=(sqrt(2*q*es*Na)/cox)
To solve eg. equation group of equations 1, 2 & 3:
Solution = solve('(vgb-phy_s)^2 = G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))', 'x = phy_s/phy_t', 'phy_t = k*T/q');
Solution.q
ans =
(T*k)/phy_t
(T*k)/phy_t
Solution.vgb
ans =
phy_s + (G*phy_t^(1/2)*(exp((2*phi_b)/phi_t) - exp(phy_s/phy_t) + exp((2*phy_s)/phy_t) - exp((2*phi_b)/phi_t)*exp(phy_s/phy_t) - (phy_s*exp(phy_s/phy_t))/phy_t + (phy_s*exp((2*phi_b)/phi_t)*exp(phy_s/phy_t))/phy_t)^(1/2))/(exp((2*phi_b)/phi_t)^(1/2)*exp(phy_s/phy_t)^(1/2))
phy_s - (G*phy_t^(1/2)*(exp((2*phi_b)/phi_t) - exp(phy_s/phy_t) + exp((2*phy_s)/phy_t) - exp((2*phi_b)/phi_t)*exp(phy_s/phy_t) - (phy_s*exp(phy_s/phy_t))/phy_t + (phy_s*exp((2*phi_b)/phi_t)*exp(phy_s/phy_t))/phy_t)^(1/2))/(exp((2*phi_b)/phi_t)^(1/2)*exp(phy_s/phy_t)^(1/2))
Solution.x
ans =
phy_s/phy_t
phy_s/phy_t
Do note that this solution is valid only for equation group of equations 1-3. For example equation group of equations 1, 2, 4 or 1, 2, 5 gives a different solution.
To solve the equation group of all 5 equations you could use this:
Solution = solve('(vgb-phy_s)^2 = G^2*phy_t*((exp(-x)+x-1)+exp(-(2*phi_b/phi_t))*(exp(x)-x-1))', 'x = phy_s/phy_t', 'phy_t = k*T/q', 'phy_b = phy_t*ln(Na/ni)', 'G = sqrt(2*q*es*Na)/cox');
However, there is no solution:
Warning: Explicit solution could not be found.
In solve at 160
Solution =
[ empty sym ]
So, I suggest that you try to find out what is wrong with your equations and then try solve again with corrected equations.