Error while solving system of equations - matlab

I received a negative MATLAB response while inserting the following requests:
syms x y z
solve (x+y==z,x-2*y==z,[x,y],'ReturnConditions', false)
I wanted to get x, and y in terms of z but, alas, I got the following error:
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 160
vc = char(v);
Error in ==> solve at 84
[eqns,vars] = getEqns(varargin{:});
Any help please?

Try using a cell array to wrap x and y instead:
>> syms x y z
>> X = solve (x+y==z,x-2*y==z,{x,y},'ReturnConditions', false);
>> X.x
ans =
z
>> X.y
ans =
0
This was required for previous versions of MATLAB. However, I'm using R2015a currently and your code works for me. I can't replicate your error. You may be using a previous version.

I think i found the hit-back to my problem.
As for solving system of two equations and two variables, we use a matrix of inputs, and outputs must be sprecified.
b=[x+y-z;x-3*y-z;]
b =
x + y - z
x - 3*y - z
[e t]=solve(b,x,y)
e =
z
t =
0
Thnk you all.

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.

Solve equation with known variables and one unknown

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.

Evaluate symbolic function in Matlab

I've defined a function in Matlab like this:
clg = 2*pi*(alpha-alphai+0.5*A(1));
where A is an array which depends on alpha.
I want to evaluate clg for aplha=0.53.
I've tried subs(clg, alpha, 0.53), but It gives me a bunch of weird errors:
Error using sym/subs>normalize (line 210)
Entries in second argument must be scalar.
Error in sym/subs>mupadsubs (line 136)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 124)
G = mupadsubs(F,X,Y);
Error in integral (line 45)
subs(clg, alpha, 0.53)
Any idea on how to achieve that?
Thanks!
I have no problem about your question.
>> syms x x1 x2
>> clg = 2*pi*(x-x1+0.5*x2);
>> subs(clg,x,0.53)
ans =
2*pi*(x2/2 - x1 + 53/100)
I am trying to reproduce the problem, but this works:
syms x
%A is a symbolic matrix that depends on x
A = inv([10*x, 2*x, 3*x;
4*x, 10, 6*x;
7*x, 8*x, 10*x])
%y is a symbolic expression defined such that
%it depends on A, but collapses to an expression of x
y = x + 0.5*A(2,2)
%subs returns a symbolic (in this case fraction) evaluation:
subs(y, x, 3)
%eval returns a numeric evaluation:
x = 3
eval(y)
(I have encountered the same error message as you in my own code, but have not yet hunted down its source.)

Unexpected result on solving some inequality in Matlab symbolic computation

Please consider this example. I would like to solve x^3 - 2x > 0. I try the following commands:
syms x;
f = #(x) x^3-2*x;
solve(f(x)>0,x)
and Matlab returns this
ans = solve([0.0 < x^3 - 2.0*x], [x])
which is not what I expect. Therefore I use
solve(f(x)+x>x,x)
which returns
ans = Dom::Interval(2^(1/2), Inf) Dom::Interval(-2^(1/2), 0)
Can someone explain that why solve works successfully only in the second case?
Try adding the Real option to solve:
solve(f(x)>0,x,'Real',1)
ans =
Dom::Interval(2^(1/2), Inf)
Dom::Interval(-2^(1/2), 0)