How can I solve an implicit equation without the symbolic toolbox? - matlab

I have an equation like this:
(5+x)^2/15+(x-4)^2/10=100
Can MATLAB solve this equation directly, without having access to the symbolic toolbox? If it can not do this, how can I resolve this problem?

It's possible, but requires some hand work.
Your function is a polynomial:
x^2/6 - (2*x)/15 + 49/15 = 100
When pulling the 100 to the left hand side, we can find the roots:
roots([1/6 -2/15 -1451/15])
ans =
24.4948
-23.6948
where the argument is specified as the prefactors in decreasing order of power.
Code with which I found the polynomial (requires the Symbolic Math toolbox):
syms x
fun = (5+x)^2/15+(x-4)^2/10-100;
f = simplify(fun);

How about using an anonymous function:
f=#(x)(5+x)^2/15+(x-4)^2/10-100;
X0=1; % initial guess
x_out=fzero(f,X0);

Related

Chain rule with symbolic function in Matlab

My question refers to the Matlab symbolic toolbox.
I'm trying to derive a symbolic function that is a function of another symbolic function. Say that I have a function x that is an unspecified function x=x(y(theta)). I'd like to take the derivative of x with respect to theta: dx/dtheta=dx/dy * dy/dtheta
In Matlab I write
syms theta y(theta);
x=sym('x(y(theta))');
diff(x,theta)
The answer I get is 0. I really cannot figure out what is wrong with the code.
Any help is greatly appreciated. Thanks!
Derivating f(g(y)):
syms x,y
f = symfun(sym('f(x)'), [x])
g = symfun(sym('g(y)'), [y])
diff(f(g(y)),y)

Solving systems of nonlinear equations

Help. I am trying to solve this system of nonlinear equations in MATLAB for a homework assignment. I have tried wolfram alpha and this online equation solver, and neither of them work.
I have tried my graphing calculator and it keeps saying non algebraic variable or expression.
These are my two equations in two unknowns:
.75*(1100)= x*10^(6.82485-943.453/(T+239.711))
25*1100=(1-x)*10^(6.88555-1175.817/(T+224.887)
I don't quite understand how to use MATLAB to solve this system. Please help.
You want the function fsolve in Matlab. Define a function myfun that returns [0,0] at the solution, then run fsolve(myfun,x0). x0 is a guess for the solution.
Define myfun:
function F = myfun(x)
F = [<put modified eqt1 here>;
<put modified eqt2 here>;];
Save it. Then solve:
x0 = [1,1];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(#myfun,x0,options) % Call solver

How to solve complex system of equations using matlab?

I have to analyze 802.11 saturation throughput using matlab, and here is my problem. I'm trying to solve parametric equations below (parameters are m,W,a) using solve function and i get
Warning: Explicit solution could not be found
How could I solve above equations using matlab?
I guess you were trying to find an analytical solution for tau and p using symbolic math. Unless you're really lucky with your parameters (e.g. m=1), there won't be an analytical solution.
If you're interested in numerical values for tau and p, I suggest you manually substitue p in the first equation, and then solve an equation of the form tau-bigFraction=0 using, e.g. fzero.
Here's how you'd use fzero to solve a simple equation kx=exp(-x), with k being a parameter.
k = 5; %# set a value for k
x = fzero(#(x)k*x-exp(-x),0); %# initial guess: x=0

Pi, Matlab Symbolic math toolbox have a bug?

Hello i have one question. When I calculate a division in matlab: x/(pi.^2)
syms x
x/(pi.^2)
ans =
(281474976710656*v)/2778046668940015
the correct answer is x/9.8696, so why is matlab giving me this result?
Is it a bug?
You have to use the vpa() command "Variable-precision arithmetic". Check this code:
syms x real; % define x as a real symbolic variable (not a complex variable)
vpa( x/(pi.^2), 5) % second argument define number of significant digits
For trigonometric expressions involving pi, it is sometimes good to define sym('pi'):
syms x real;
pi_s = sym('pi');
expr = x/pi_s^2
I try to always use the 'real' tag when using the symbolic toolbox. If you do not use it you are going to see a lot of complex conjugates and other things that are not important for your problem, because x is probably real variable.
Hope this helps,
No it is not a bug:
2778046668940015/281474976710656 = 9.8696

Get the derivative of a function_handle in MATLAB

Is it possible to get the derivative of a function_handle as a other function_handle?
Like:
fun1 = #(x) x^2;
% do that ...
disp(fun2);
#(x) x*2
I know how to find the derivative of a symbolic function but I can't convert a function_handle to a symbolic function.
I'm new to matlab and I couldn't find any way on doing that. Thanks in advance.
The short answer is "No." MATLAB has no idea what the contents of the function_handle mean in a symbolic sense. You're better off creating it using syms in first place.
A longer answer would be either to use the Symbolic Math Toolbox, as suggested by #A Danesh, or an approximation, as suggested by #Andrey.
However, if you're always working with polynomials, then you can store the coefficients as an array and use the following functions:
polyval to evaluate
conv to multiply
deconv to divide
polyder to differentiate
polyint to integrate
syms x
f = #(x) x^2 + 1;
diff(f,x)
Ans:
2*x + 1
You can get an approximation function:
delta = 0.0001;
d = #(x)( (fun1(x+delta) - fun1(x))./delta)
you can't analytically from a function handle.
but if you got the symbolic math toolbox you can derivate the symbolic function and create a function handle from the result.