MATLAB error when solving simultaneous equations - matlab

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.

Related

Trying to solve a quadratic equation in 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);

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)

nlinfit Dimension Error

I am using nonlinearfit tool in matlab.
I keep getting the following error:
Error using nlinfit (line 210)
MODELFUN must be a function that returns a vector of fitted values the same size as Y (1-by-100). The model function you provided returned a result that was 1-by-2.
One common reason for a size mismatch is using matrix operators (, /, ^) in your function instead of the corresponding element-wise operators (., ./, .^).
I found this question very similar to mine, but still I get the same error. I have tried calculating myfun on the console while using a vector as an input, which gives me output of correct dimension. It will be ton of help if anybody can point out the mistake.
% Defining the function
myfun = #(t,b)exp(t.*b(1)+b(2));
[y_a] = arrayfun(myfun,x_a);
% Using nonlinear least square minimization
beta0 = [1 1];
nlinfit(x,y,myfun, beta0)
Thanks in advance...:)
Edit: Found this to be working.
g = fittype('exp(k*x + a)');
[fit1,gof,fitinfo] = fit(x',y',g,'StartPoint',[1 1]);
The function used in nlinfit takes the parameter vector as its first argument, then the independent data vector. You want,
myfun = #(b,t)exp(t.*b(1)+b(2));
Note that you could just use * rather than .* in this instance too.

Calculating derivative and integration using matlab

I am trying to find second derivative of a function, but while initializing my symbols i am getting the following error:
Error using ==> subsindex
Function 'subsindex' is not defined for values of class 'sym'.
The commands I am using are:
syms x a b c L;
u = (a*x(x-L))+(b*x((x^2)-(L^2)))+(c*x((x^3)-(L^3)));
"u" is my function.
I don't know much about MATLAB's symbolic capabilities, but that error is coming from the pieces like
x(x-L)
which MATLAB is interpreting as an indexing operation. Did you mean multiplication there? I.e.
x*(x-L)

Matrix dimension error while calling mldivide in MATLAB

I am getting this error while running my code:
Error using ==> mldivide Matrix dimensions must agree.
Here is my code :
%make the plots of phase and group velocity vs discreteness of the grid
c=1;
a=input('Please enter the ratio cdt/dx : ')
figure(1)
R=2:40;
plot(R,phase_vel(R,a)/c)
xlabel('R=l/dx')
ylabel('u_phase/c')
%figure(2)
%plot(group_vel(R,a),R,0,40)
%xlabel('R=l/dx')
%ylabel('u_group/c')
and here are my functions :
function phase_velocity = phase_vel(R,a)
%numerical phase velocity of the discrete wave
c=1;
phase_velocity=(2*pi*c)/(R*knum(R,a));
end
function group_velocity =group_vel(R,a )
%numerical group velocity of the discrete wave
c=1;
group_velocity=(a*sin(knum(R,a)))/(sin(2*pi*a/R))
end
function knumber = knum(R,a)
%This is the k wave number
knumber=acos((1/a)^2*(cos(2*pi*a/R)-1)+1);
end
How can I resolve this error?
EDIT: I used . operator in every equation and i changed the limits of R=4:40
If your goal is to apply your formulas to each individual value in the vector R then you should be performing all of your computations using the element-wise arithmetic operators .*, ./, and .^ instead of the matrix operators *, /, and ^.
Your error is probably occurring in the first call to your function knum, specifically when you try to compute 2*pi*a/R. Since 2*pi*a is a single scalar value, you get an error when trying to perform matrix right division / using the row vector R. The really weird thing is the error message:
??? Error using ==> mldivide
Matrix dimensions must agree.
which implies you are using the matrix left division operator \, which you clearly aren't. I tested this in MATLAB R2010b and I get the same incorrect function name appearing in my message. I think this may just be a typo in the error message, and I've dropped a note to the MATLAB folks to take a look at it and clear it up.
I don't have the Symbolic Math Toolbox, but your problem seems to be that you are using plot, a function which can deal with arrays of numbers, and feeding it the result of a symbolic calculation. Have a look at the Matlab Help, where the Topic Creating Plots of Symbolic Functions suggests using ezplot(). Alternatively you need to evaluate your symbolic expression for certain input values to create an array of numbers that plot can deal with - but you can't use double() for that since it wouldn't know what numbers to plug into your variables.