Solving an equation involving integral in Matlab - matlab

How to solve following question in Matlab.
Let α ∈ (−∞,∞).
Then how to solve: 1 + 4 {D1(α)−1} / α = 0.4615 in MATLAB?
where D1(α) is Debye function defined by:
D1(α) = 1/α ∫t/(et−1)dt
(integral from [0 α])

MATLAB won't give you an exact answer for this but you could use.
syms alpha t real
D = #(alpha)1/alpha * int(t / (exp(t)-1), t, 0, alpha);
solve(1 + 4*(D(alpha) - 1)/alpha == 0.4615, alpha)
output is
Warning: Cannot solve symbolically. Returning a numeric approximation instead.
> In solve (line 303)
ans =
5.0770060488781283390761360077113
Edit: After the LaTeX was interpreted and posted I realized I read it wrong. I missed a pair of parenthesis. This issue has been fixed.

Related

MATLAB Converting symbolic expressing to numeric via double

I am having a bit of an issue with my code and I can't find a good way to solve it. The issue seems to lie in the usage of double to convert a symbolic expression into a numeric expression that could be plotted using double.
Here is my code:
P1 = [-1.5, -2]
P2 = [2, 2]
P3 = [-2.5, 2.5]
P4 = [2, -1]
syms x
syms y
c = 299792.458e3
r1i = sqrt((P1(1,1) - x)^2 + (P1(1,2) - y)^2)
r2i = sqrt((P2(1,1) - x)^2 + (P2(1,2) - y)^2)
t21 = -3.7294e-6
S = double(solve(t21 == (r2i-r1i)/c, y))
However, this produces the error:
Error using symengine
DOUBLE cannot convert the input expression into a double array.
Error in sym/double (line 613)
Xstr = mupadmex('symobj::double', S.s, 0);
Error (line 18)
S = double(solve(sym(t21) == (r2i-r1i)/c, y))
I've done a bit of Googling, but I can't find any other site talking about a similar issue. Would anyone be able to assist me? Thank you so much for your time and help in advanced!
This question has been marked as a duplicate, but it doesn't quite fit the other identified link because I'm trying to plot the equation. It should produce two hyperbolas. None of the methods suggested help with the plotting

Matlab, cannot turn sym to double

I am trying to solve a variable in an equation (syms x), I've simplified the equation. I am trying to store the value in P_9, a 1x1000 matrix by converting from a symbol to a double and am getting the error below. It is giving me a symbol of 0x0, which is where I think my error lies.
Please help me troubleshoot my code. Many thanks!
number = 1000;
P_9 = zeros(1,number);
A_t=0.67;
A_e = linspace(0,10,number);
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = solve(eqn,x);
P_9(n) = double(solx);
end
Warning: Explicit solution could not be found.
In solve at 179
In HW4 at 74
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in HW4 (line 76)
P_9(n) = double(solx);
You certainly have an equation, where x can't be isolated.
For example it is impossible to isolate x in tan(x) + x == 1. So if you try to solve this equation analyticaly, matlab will tell you that x can't be isolated and therefore there is no explicit analytical solution.
So instead of using an analytical method to solve your equation, you need to use a numerical method, it's less "sexy" but this time you will be able to solve your equation.
Life is well done, matlab already integrate a numerical solver: vpasolve.
So your code will look like:
for n=1:number
%% find p9
syms x
eqn = x + 5 == A_t/A_e(n);
solx = vpasolve(eqn,x);
P_9(n) = double(solx);
end

MATLAB not evaluating integral

MATLAB gives me the same expression back.
Here's my code
syms tau alpha phi
f = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
F=f*sin(phi);
int(F,phi, [pi/2, acos(tau/alpha)])
I did plug in the values of variables tau and alpha but it still gives me the same expression back. Anyone know how to solve it? Or some other numerical method which could give me answer in these symbols? Will wolfram-mathematica help?
Mathematica
Integrate[(Sign[alpha*Cos[phi]]*(Abs[alpha*Cos[phi]] - 5/2*
(alpha*Cos[phi])^2))*Sin[phi], {phi, Pi/2, ArcCos[tau/alpha]}]
(* ConditionalExpression[-(tau^2/(2 alpha)) + (5 alpha^3 Abs[tau]^3)/
(6 Abs[alpha]^4), ArcCos[tau/alpha] \[Element] Reals] *)
Thus the result is the expression inside the ConditionalExpression if the ArcCos[tau/alpha] is an element of the reals.
If you could specify something like -1<=tau/alpha<=1 && 0<=tau then it can provide an even simpler result, (tau^2 (-3 + 5 tau))/(6 alpha)
Please verify this before you depend on it.
Something like
syms tau alpha phi
f = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
will just generate another symbolic variable. Your goal however is to specify a symbolic function and for that you have to specify the function arguments:
syms tau alpha phi
f(tau,alpha,phi) = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
F(tau,alpha,phi) =f*sin(phi);
Then you can calculate the integral with
R = int(F,phi, [pi/2, acos(tau/alpha)])
Since your integral is dependent on tau and alpha, R is again a symbolic function R(tau,alpha).
Alternatively you could just specify the arguments at the end, e.g.
syms tau alpha phi
f = sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2);
F=f*sin(phi);
R(tau,alpha) = int(F,phi, [pi/2, acos(tau/alpha)])
But I personally find that less clean.
Note that you could also write F directly as
F(tau,alpha,phi) = sin(phi)*(sign(alpha*cos(phi))*(abs(alpha*cos(phi)) - 2.5*(alpha*cos(phi))^2));

maxima multiple trigonometric equations

I'm trying to solve an equation using maxima 13.04.2 but the answer isn't what I expect.
Example:
y2=A2*cos(2*pi*f2*t+phase2) we know A2=.4,f2=6.4951,t=1, trying to find **phase2**
y2=.4*cos(2*pi*6.4951+phase2)
I tried to solve the y2 equation for phase2 in maxima but it got rid of the cos function
kill(all);
A:A; phase:phase; solve(A*cos(2*pi*f*t+phase)=0,phase);
The answer that came back was
I thought something like this was suppose to come back
y2 = A2×cos(2πf2t + φ2) ⇒
y2/A2 = cos(2πf2t + φ2) ⇒
arccos(y2/A2) = 2πf2t + φ2 ⇒
arccos(y2/A2) - 2πf2t = φ2
so I could then plug in the vales
A2 = 0.4, f2 = 6.4951, t = 1 and get the phase
Any ideas how to get maxima to get the correct format?
PS: Yes I know I can do it by hand but I have thousands of equations like this and I plan on using octave arrays to call maxima to solve them and bring the answers back into octave.
Well, it seems straightforward.
(%i1) e:A*cos(2*%pi*f*t + phi) = y;
(%o1) cos(2 %pi f t + phi) A = y
(%i2) solve (e, phi);
solve: using arc-trig functions to get a solution.
Some solutions will be lost.
y
(%o2) [phi = acos(-) - 2 %pi f t]
A
(%i3) subst ([A = 0.4, f = 6.4951, t = 1], %o2);
(%o3) [phi = acos(2.5 y) - 12.9902 %pi]
Couple of notes. (1) solve can solve this equation, which is good, but bear in mind that it is fairly limited in its capability, and you can probably make up other equations that it can't solve. There are some other options for solving equations in Maxima, but it general that is a weak area. (2) Maybe instead of switching back and forth between Maxima and Octave, you can just code equation %o2 in Octave and evaluate that for different values of the parameters.

Matlab error with nlinfit

I'm attempting to get the nonlinear least squares fit of the following equation:
y = 1 / (1 + a (ln(duration)^b))
The data I wish to fit this to is presented below, as is my attempt to use the nlinfit function to solve it...
x = [1.99000000000000;3.01000000000000;4.01000000000000;5.09000000000000;5.77000000000000;6.85000000000000;7.72000000000000;8.87000000000000;9.56000000000000;];
y = [1;1;0.800000000000000;0.730000000000000;0.470000000000000;0.230000000000000;0.270000000000000;0.100000000000000;0.100000000000000;];
plot(x,y,'o','linestyle','none');
p(1) = 1;
p(2) = 1;
fun = #(p,x) 1 / (1 + p(1).*(log(x).^p(2)));
myfit = nlinfit(x,y,fun,p);
My problem seems to be with defining the fourth required input for the nlinfit function ('p' in the example above). The documentation doesn't give a clear explanation of what is needed for this to work, and I can't solve the problem based on the error messages I receive:
??? Error using ==> nlinfit at 128 MODELFUN should return a vector of fitted values the same length as Y.
Error in ==> fitting at 14 myfit = nlinfit(x,y,fun,p);
I've tried setting p to repmat(1,9,1) and repmat(1,1,9), but neither of these seem to solve my problem. Any help would be greatly appreciated!
I believe you forgot the dot to do element-wise deletion in the function:
fun = #(p,x) 1 ./ (1 + p(1).*(log(x).^p(2)));
^