Solve equation with known variables and one unknown - matlab

I'm trying to solve this equation in Matlab
dT=((-A-B*C+D*./E)
where C=sin(dT). dT is unknown. A, B, D, and E are known variables. Using Matlab's solve function:
Ans=solve(dT==((-gra-H_vap*m_lg+grb*./ro_cp),dT);
But I receive an error message. How do I solve this equation?

You haven't given us any specifics on the values of your known parameters, and I also believe that D*/E in your example were intended to be a more valid expression.
Anyway, here is an example of how you make use of the symbolic solver solve:
syms dT
A = 1
B = 2
D = [1 2]
E = [3 4]
eqn = -A - B*sin(dT) + D/E - dT == 0
soldT = solve(eqn,dT)
which produces the following output
% ...
eqn =
- dT - 2*sin(dT) - 14/25 == 0
% ...
soldT =
-0.18739659458654612052194305796251
See also the language docs for solve.

Related

How to solve for a system of equations in MATLAB when the variables are actually symbolic functions?

>> syms x v(x) w(x);
>> eq1 = 2*v + 3*w == 4;
>> eq2 = 5*v + 4*w == 3;
>> sol = solve([eq1,eq2],[v,w])
I tried to implement this code in MATLAB, but error flashes out as "The second argument must be a vector of symbolic variables." I have tried similar things in Python using SymPy, but never such error comes. How to correct out this?
Have a look at the help file for the multivariate case of solve and the example in the help file
openExample('symbolic/SolveMultivariateEquationsAndAssignOutputsToStructureExample')
Applied to your problem
syms v w;
eq1 = [2*v + 3*w == 4;5*v + 4*w == 3];
sol = solve(eq1)
sol.v
sol.w
but if you just want to solve for v w then you could use e.g.
[2 3;5 4]\[4;3]

Solving integral for x in MATLAB, where x is bound and part of the integrand

I am trying to solve an equation for x in Matlab, but keep getting the error:
Empty sym: 0-by-1
The equation has an integral, where x is the upper bound and also part of the integrand 1. The code I use is the following:
a = 0.2; b= 10; c = -10; d = 15; mu = 3; sig = 1;
syms x t
eqn = 0 == a + b*normcdf(x,mu,sig)+c*int( normcdf(d + x - t,mu,sig)*normpdf(t,mu,sig),t,0,x);
A = vpasolve(eqn,x)
Any hints on where I am wrong?
I believe that the symbolic toolbox may not be good enough to solve that integral... Maybe some assume or some other trick can do the job, I personally could not find the way.
However, to test if this is solvable, I tried Wolfram Alpha. It gives a result, that you can use.
eq1=a + b*normcdf(x,mu,sig);
resint=c*(t^3*(d - t + x)*erfc((mu - x)/(sqrt(2)*sig)))/(4*sig*exp((-mu + x)^2/(2*sig^2))*sqrt(2*pi));
A=vpasolve(eq1+subs(resint,t,x)-subs(resint,t,0) ==0)
gives 1.285643225712432599485355373093 in my PC.

Error using solve(): Single character variable names work, but longer names give error

Does Anybody know why this code :
syms pi41 r C2 sigma mu C3 theta
pi41 = solve('(-2*pi41-2*r*C2+3*(sigma^2-2*mu)/sigma*C3)*theta^2','pi41')
has error like this :
Error using mupadengine/feval (line 163)
Invalid argument.
Error in solve (line 294)
sol = eng.feval('solve', eqns, vars, solveOptions);
But with simple characters like this code :
syms a b c d e f x
x = solve('(-2*x-2*a*b+3*(d^2-2*e)/d*c)*f^2','x')
has not ?
I'll appreciate any help.
build two vectors, one with the equations and one with the unknowns
syms foo bar
eq = [ foo + bar == 5;
2*foo - bar == 4];
unknowns = [foo, bar];
then use solve
sol = solve(eq, unknowns)
or in your case
syms pi41 r C2 sigma mu C3 theta
eq = (-2*pi41-2*r*C2+3*(sigma^2-2*mu)/sigma*C3)*theta^2 == 0;
unknowns = [pi41];
sol = solve(eq, unknowns);
results in
sol =
- C2*r - (C3*(- 3*sigma^2 + 6*mu))/(2*sigma)
you also might want to specify/make sure that sigma is non zero....
...in Matlab 2015b

Using fsolve to solve Differential Equation with Varying Parameters

I am using the following code to produce a numerical solution to a system of ODE's with 6 boundary conditions.
I am using the initial conditions to obtain a solution but I must vary three other conditions in order to find the true solution. The function I have is as follows:
function diff = prob5diff(M,Fx,Fy)
u0 = [pi/2 0 0]';
sSpan = [0 13];
p = #(t,u) prob5(t,u,M,Fx,Fy);
options = odeset('reltol',1e-6,'abstol',1e-6);
[s,u] = ode45(p,sSpan,u0,options);
L = length(s);
x = u(:,2); y = u(:,3); theta = u(:,1);
diff(1) = x(L) - 5;
diff(2) = y(L);
diff(3) = theta(L) + pi/2;
end
Ultimately, different values of M,Fx, and Fy will produce different solutions and I would like a solution such that the values in diff are as close to zero as possible so I want fsolve to iterate through different values of M,Fx, and Fy
I am receiving the following error: when I call it in this way:
opt = optimset('Display','iter','TolFun',1e-6);
guess = [1;1;1];
soln = fsolve(#prob5diff,guess,opt);
Error in line:
soln = fsolve(#prob5diff,guess,opt);
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Thanks!
One problem is that you have to call fsolve on prob5diff which takes a single vector input since your guess is a single vector:
prob5diff(x)
M = x(1);
Fx = x(2);
Fy = x(3);

Matlab - "Could not extract differential variables to solve for" in dsolve

I am trying to solve a RLC circuit as a symbolic differential equation in MATLAB using dsolve, the equation being
U(t) = L*Q''(t) + R*Q'(t) + (1/C)*Q(t)
with the initial conditions
Q(0) = 0
Q'(0) = 0
and
U(t) = 10*sin(2*t)
L = 1
R = 0
C = 1/4
While this works ...
When I implement it explicitly (and using strings) as
Q = dsolve('D2Q(t) + 4*Q(t) = 10*sin(2*t)', 'DQ(0)=0, Q(0)=0');
Q = simplify(Q);
I'll get
Q =
5 sin(2 t) 5 t cos(2 t)
---------- - ------------
4 2
which is correct.
... this does not.
For purely esoteric reasons I tried computing it directly using symbolic equations, as the documentation on dsolve stated it could be done.
So starting with
syms L R C t Q(t)
U = sym('10')*sin(sym('2')*t)
DEQ = L*diff(Q(t),t,2) + R*diff(Q(t),t) + (1/C)*Q(t)
DEQ = subs(DEQ, [L R C], [sym('1'), sym('0'), sym('1/4')])
eqn = (U == DEQ)
I receive
eqn =
10*sin(2*t) == 4*Q(t) + diff(Q(t), t, t)
Which is correct. If I now feed it into dsolve though, using
Q = dsolve(eqn, ...
Q(t) == 0, ...
diff(Q(t),t) == 0);
Matlab throws the error
Error using symengine (line 58)
Could not extract differential variables to solve for. Use 'solve' or 'vpasolve'
to compute the solutions of non-differential equations.
Why is that?
It looks like you're using sym/diff and symfuns incorrectly. Q(t) is what is referred to as an arbitrary (help sym/diff uses the term "abstract" instead) symbolic function, i.e., a function with no definition. Your function's name is Q (think of it as a function handle) and it is represented by the abstract formula Q(t), which just means that it's a function of t. When you want to take the derivative of an abstract function, pass in the name of the function - in your case, Q (the online documentation makes this slightly clearer, but not really). When you want evaluate the function use the formula, e.g., Q(0), the output of which is a sym rather than a symfun.
Here is how I might write the code for your second case:
syms L R C t Q(t)
U = 10*sin(2*t); % No need to wrap integer or exactly-represenable values in sym
dQ = diff(Q,t);
d2Q = diff(dQ,t);
DEQ = L*d2Q + R*dQ + Q/C;
DEQ = subs(DEQ, {L, R, C}, {1, 0, 1/4});
eqn = (U == DEQ);
Q = dsolve(eqn, Q(0) == 0, dQ(0) == 0);
Q = simplify(Q)
which returns
Q =
(5*sin(2*t))/4 - (5*t*cos(2*t))/2
You also forgot to evaluate your initial conditions at zero in the second case so I fixed that too. By the way, in current versions of Matlab you should be using the pure symbolic form for symbolic math (as opposed to strings).