Trying to solve a quadratic equation in Matlab - matlab

I have just started to use Matlab and I am struggling to solve quadratic equations. I am studying with the following guide:
A guide to Matlab for beginners and experienced users
Link to a preview.
I tried to solve the equation provided by the book (page 17). Despite typing exactly what the book instructed, I did not get the desired output. Note that firstly I tried with solve command and secondly with fzero but none worked out:
>> clear
>> syms x
>> solve ('x^2 - 2*x - 4 = 0')
Error using solve>getEqns (line 418)
List of equations must not be empty.
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
>> fzero ('x^2 - 2*x - 4 = 0')
Error using fzero (line 121)
FZERO requires at least two input arguments or a structure
with valid fields.
I do not understand the correction 'List of equations must not be empty' after using solve command. Besides, when I tried to use fzero command I got 'FZERO requires at least two input arguments or a structure with valid fields' which I did not get the hang of either. I looked for an alternative and I found x could be enclosed in parenthesis:
>> solve ('x^2 - 2*x - 4 = 0', x)
Error using solve>getEqns (line 418)
List of equations must not be empty.
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
But again 'List of equations must not be empty' came as a result.
Please take into consideration the fact that I am a beginner and I understand this question may be too obvious to you, however I have been trying for a while and could not figure it out.
Thanks

Based on the following link, it looks like specifying your equation as a string (char vector) might be your issue. Equations and systems solver in MATLAB. In your situation, I would try the following:
clear
syms x
solve(x^2 - 2*x - 4 == 0, x);

Related

solving nonlinear equations including max function

I want to solve this system of equations using MATLAB:
I solved the second equation and put it in the first one. Then I tried this:
syms v_star
eqn = max((1.8+0.81*v_star), (-1+0.9*v_star)) == v_star;
solx = solve(eqn,v_star)
And I get this error:
Error using symengine
Input arguments must be convertible to floating-point numbers.
How can I solve this system of equations?

My symbolic polynomial it's not a symbolic polynomial?! Weird error

I have an symbolic expression like this:
syms h
g=exp(h)+h*exp(h)+h^2*exp(h);
And I really need to extract an polynomial from it, so I wrote:
polyn=coeffs(g,exp(h))
which gives me an symbolic polynomial in h equal to: h^2+h+1.
Now, I want to extract the coefficients from this symbolic polynomial:
coeff=sym2poly(polyn);
But I'm getting the error message:
"Error using symengine (line 58)
Expression is not a polynomial."
So, either has a bug in my computer (which there's a chance to) or Matlab is not recognizing my "symbolic polynomial" as an symb. polyn, actually.
I have to mention that if I type:
polyn=h^2+h+1;
coeff=sym2poly(polyn);
I dont get any error at all! However, since I am programming, I cant type this polynomial, so I need to obtain it in the way I did.
Is there something I can do here??
Here is the solution that works for me:
syms h
g= exp(h)+h*exp(h)+h^2*exp(h);
polyn= coeffs(g,exp(h))
polyn= sym(char(polyn));
coeff= sym2poly(polyn)

Error when solving symbolic system of ODEs using dsolve

I have been trying to use the command dsolve in Matlab to solve a set of ODEs, but I am getting these errors:
Error in dsolve>mupadDsolve (line 332)
T = feval(symengine,'symobj::dsolve',sys,x,options);
Error in dsolve (line 193)
sol = mupadDsolve(args, options);
Below is the code if someone wants to take a look at it:
syms t b1 b2 k1 k2;
A=0.5;
m1=3;m2=4;w=6;
y=A*sin(w*t);
xt=dsolve('m1*D2x1+b1*((Dx1)-Dy)+k1*(x1-y)+b2*((Dx1)-(Dx2))+k2*(x1-x2)=0','m2*D2x2+b2*((Dx2)-(Dx1))+k2*(x2- x1)=0','x1(0)=0','Dx1(0)=0','x2(0)=0','Dx2(0)=0');
Could someone please help me with that?
Thank you all very much
Quoting from the dsolve documentation:
The names of symbolic variables used in differential equations should not contain the letter D because dsolve assumes that D is a differential operator and any character immediately following D is a dependent variable.
As such, the Dy in your argument string is telling dsolve there is some unknown function y to solve for but without an initial condition (in addition to it being an indeterminant system).
To fix the issue, define the derivative a y outside dsolve and assign it to a variable without D:
syms t b1 b2 k1 k2;
A=0.5;
m1=3;m2=4;w=6;
y=A*sin(w*t);
dydt = diff(y,1);
xt=dsolve('m1*D2x1+b1*((Dx1)-dydt)+k1*(x1-y)+b2*((Dx1)-(Dx2))+k2*(x1-x2)=0','m2*D2x2+b2*((Dx2)-(Dx1))+k2*(x2-x1)=0','x1(0)=0','Dx1(0)=0','x2(0)=0','Dx2(0)=0');
This version of the code runs for me ... and runs and runs. While it does run, I think more constants need to be defined in order for an answer to be generated.

Error using ==> mtimes MATLAB

I am trying to numerically integrate a function on MATLAB (I am on MATLAB 7.7). Here is my code.
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39*(cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2 + 2/3*sin(t).^2);
quad(fun,-pi/6,pi/6)
Unfortunately I am not following the error message. Can anyone help me please?
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
#(t)(cos(t)./sqrt(3)-sin(t)).^39*cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2+2/3*sin(t).^2)
Error in ==> quad at 77
y = f(x, varargin{:});
I have tried to see if the function definition is right. It seems to work for me:
fun(1)
ans =
-1.4078e-007
which the correct value I would expect when evaluated at 1. I have done several trials with various inputs, the function fun() seems to compute them alright!
p.s.: I have used quad() before this. It has worked well for me previously.
Your function works for scalar inputs, but not for vectors:
>> fun(1:5)
Error using *
Inner matrix dimensions must agree.
You need to change your function to do elementwise multiplication:
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39.*(cos(t)./sqrt(3)+sin(t)).^63.*sqrt(2*cos(t).^2 + 2/3.*sin(t).^2);

MATLAB error when solving simultaneous equations

I am running a MATLAB code which solves a set of non-linear simultaneous equations. The code can be found here. The data input for the code (in excel) can be found here.
I encounter the following error:
Error using sym/subsasgn (line 733)
Indexed assignment to empty SYM objects is supported only in the 0-by-0 case.
Error in Solution (line 69)
x(i,:) = (b(i,1) - b0)./(c(i,1)*x0) + c0/c(i,1);
Does anyone have any idea how I can resolve this?
When declaring a symbolic variable, you have to specify the dimensions, otherwise it's a scalar. Instead of syms x; use sym and set the dimension argument:
x=sym('x',[3,4])
Replace [3,4] with the dimensions you want.