I have really big problem solving equations like this one (simplified one):
a*cos(x) + b*sin(x) = cos(x) + sin(x), with respect to a,b.
In the equation can stand expressions of: sin(x), cos(x), exp(C*x) and polynomials.
I need to get solution in this form: [a b] = [1 1].
Only solution I can reach now is: b = 0, a = (cos(x) + sin(x))/cos(x).
I was trying hard to find something, but I did not find anything. I really appreciate any help! Thank you!
I guess I get it. If I specify the variables with assume:
assume(a, 'real')
assume(b, 'real')
I get the result I am looking for.
Related
Let
syms h
g=exp(h)+h*exp(h)+h^2
so, the coefficients of g in respect to his given by the function coeffs:
coeffs(g,h)
and it returns:
[exp(h),exp(h),1]
It's like this function deals with the symbolic expression like a polynomial in h.
The problem is that this function doesnt return the zero coefficients, so if I have an
g=h*exp(h)+h^2
the function returns only:
[exp(h), 1].
But what I need is:
[0,exp(h), 1]
So, what can I do here?
I went to a lot of topics on SO and the solution is:
syms h
g=h*exp(h)+h^2
m = eval(feval(symengine,'coeff',g,h,'All'));
I hate to be one of those posters that lectures, but this is listed right in the Matlab help (help coeff) or online
coeffs(g,h, 'All')
I have a system that looks something like this:
z1 = 5*x1 + x2*cos(x3)
z2 = x1*sin(x3) + 3*x2
z3 = 3*x1 - 2*x2
which is the transformation of a system of differential equations (just to give some context, I don't think you really need to know this). Now I would like to find the inverse of this transformation which would look something like:
x1 = ...(stuff with z1, z2, z3)...
x2 = ...
x3 = ...
now I guess with this system it is relatively easy to find it manually but I have to do the same thing with a 7 variable 7 equation system and it would get pretty tedious to do.
I cannot find a way to get Matlab to do this, can anybody help?
Maybe this could work:
You could get an approximate solution by taylor expanding the trigonometric functions and then doing Gaussian elimination.
Gaussian elimination can be done using rref in Matlab if the problem can be put on the form (using random numbers):
x+3y+4z = 5
2x+y+6z= 4
8x+2y+7z=3
Then Ax=b, A=[1 3 4;2 1 6;8 2 7], b=[5;4;3]
To find x do: E=[A b]
x=rref(E)
EDIT: Sorry, I see now that you don't have numbers for z1, z2.. so this is not really a solution for your case.
I have an equation of the form y=ao+a1logx+a2log(2/x); Is there away to fit this kind of equations?
I tried to use polyfit but finding the coefficients ao,a1 and a2 is difficult for me.
Please Help me.
What toolboxes are available to you?
The easiest way would probably be the cftool. (Type it into your command window) if you have the curve fitting toolbox. But polyfit should do as well.
The main problem I see: Your coefficients are not independent of one another. Because log(2/x) is equal to log(2) - log(x) your equation becomes:
y = ao + a1*log(x) + a2*log(2) - a2*log(x);
which is equivalent to:
y = bo + b1*log(x);
Try that one.
I am now trying to solve an exponential equation in MATLAB as a part of my assignment. It is easy to see that the equation
exp(-t)+t*exp(-t)-n=0
would have two solutions, one greater than zero and one smaller.
However, using just the solve function, MATLAB returns something called lambertw function and it can only eval() to the solution below zero, which happens not to be the one I want for the answer. Could anyone help me here?
Thanks in advance for all the answers and comments!
p.s. As an alternative, I am thinking about using Newton-Raphson method to solve it, but I wonder how is the speed comparing to solve()?
Ziyao Wei
In the code below, I am numerically solving the equation for n=0.5 (the constant), but you it should be similar for other values you choose.
Note how the SOLVE function only returned the first solution found. Therefore I am calling the MuPAD engine directly, and specifying each time an interval in which to search for the solution:
%# lets plot the function: f(x) = exp(-x)+x*exp(-x)
h(1) = ezplot('0.5', [-1.5 10]); hold on
h(2) = ezplot('exp(-x)+x.*exp(-x)', [-1.5 10]);
set(h(1), 'LineStyle',':', 'Color','r')
legend(h, 'y = 0.5', 'y = exp(-x)+x.*exp(-x)')
%# The numeric solver only returns the first solution that it finds
x = solve('exp(-x)+x*exp(-x)=0.5')
x = vpa(x)
%# we can call the MuPAD solver and give the interval where solution can be found
x1 = evalin(symengine, 'numeric::solve(exp(-x)+x*exp(-x)=0.5, x = -1..0)')
x2 = evalin(symengine, 'numeric::solve(exp(-x)+x*exp(-x)=0.5, x = 0..3)')
%# show the solutions on the plot
plot([x1 x2], 0.5, 'ro')
The solution returned by SOLVE:
x =
- 1.0*lambertw(0, -1/(2*exp(1))) - 1.0
x =
-0.76803904701346556525568352607755
MuPAD numeric solutions:
x1 =
-0.76803904701346556525568352607755
x2 =
1.6783469900166606534128845120945
lambertw is the function that you need. However, it is a multivalued function and has several branches. You need to choose the right branch for your answer. See my answer to another question on how to choose a different branch for the solution.
The answer provided by matlab is correct but it gives you only one branch.
To get another branch, use the answer provided but replace lambertw(x) by lambertw(k,x) for different values of k.
See the doc of lambertw for more details.
You can have a look at the Lambert W function on mathworld to learn more and visualize the different branches.
I have a system of 2 equations in 2 unknowns that I want to solve using MATLAB but don't know exactly how to program. I've been given some information about a gamma distribution (mean of 1.86, 90% interval between 1.61 and 2.11) and ultimately want to get the mean and variance. I know that I could use the normal approximation but I'd rather solve for A and B, the shape and scale parameters of the gamma distribution, and find the mean and variance that way. In pseudo-MATLAB code I would want to solve this:
gamcdf(2.11, A, B) - gamcdf(1.61, A, B) = 0.90;
A*B = 1.86;
How would you go about solving this? I have the symbolic math toolbox if that helps.
The mean is A*B. So can you solve for perhaps A in terms of the mean(mu) and B?
A = mu/B
Of course, this does no good unless you knew B. Or does it?
Look at your first expression. Can you substitute?
gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) = 0.90
Does this get you any closer? Perhaps. There will be no useful symbolic solution available, except in terms of the incomplete gamma function itself. How do you solve a single equation numerically in one unknown in matlab? Use fzero.
Of course, fzero looks for a zero value. But by subtracting 0.90, that is resolved.
Can we define a function that fzero can use? Use a function handle.
>> mu = 1.86;
>> gamfun = #(B) gamcdf(2.11, mu/B, B) - gamcdf(1.61, mu/B, B) - 0.90;
So try it. Before we do that, I always recommend plotting things.
>> ezplot(gamfun)
Hmm. That plot suggests that it might be difficult to find a zero of your function. If you do try it, you will find that good starting values for fzero are necessary here.
Sorry about my first try. Better starting values for fzero, plus some more plotting does give a gamma distribution that yields the desired shape.
>> B = fzero(gamfun,[.0000001,.1])
B =
0.0124760672290871
>> A = mu/B
A =
149.085442218805
>> ezplot(#(x) gampdf(x,A,B))
In fact this is a very "normal", i.e, Gaussian, looking curve.