Noncomformant arguments error although using element-wise operations - octave

I am using element-wise operation and am still getting the error nonconformant arguments in Octave. The code has been given below
function g = sigmoid(z)
g = zeros(size(z));
% Instructions: Compute the sigmoid of each value of z (z can be a
% matrix,vector or scalar).
g = 1./(1+exp(-z));
end
The error is
"error: sigmoid: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x4)"
Could someone please help? Thanks

Related

Octave Error: Subscript indices must be integers or boolean

I was trying to solve a system of non-linear equations and check the stability of its equilibrium points. Initially, I declared two equations
adot = -a + 2*a^3 + b
bdot = -a -b
By equating both the equations with 0, I get the equilibrium points. Now, I am trying to get the Jacobian of the [adot;bdot] matrix using the jacobian([adot;bdot],[a,b]) method of symbolic package in Octave, which should just return a matrix whose items are the partial derivatives of "adot" and "bdot" w.r.t. "a" and "b", but it gives the following error
error: subscript indices must be integers or boolean
Can anyone tell me where I'm going wrong with this?
Edit: I'm adding the complete code down below:
pkg load symbolic
syms x y
xdot = -x + 2*x^3 + y;
ydot = -x - y;
[xeq,yeq] = solve(xdot==0,ydot==0);
xeq = double(xeq);
yeq = double(yeq);
jacobian_matrix = jacobian([xdot;ydot]);
At this point I'm getting the above mentioned error. The values in the [xeq,yeq] matrix are the equilibrium points of the system, which are to be used later on.

How to use symprod with symbolic arrays?

I would like to have this equation solved symbolically:
x_i = x_0 + \prod_{j = 0}^{i-1}(a_{3-j})
I wrote the following script, which works until I call symprod:
try
pkg load symbolic
end
a = sym('a', [1 3]);
syms x0 i
x0*symprod(a(i), i, [1 3])
The error message says:
error: subscript indices must be integers or boolean
However, this works:
a(2)
ans = (sym) a12
What is wrong with my code?
(I also tried with Matlab Symbolic Toolbox; does not work either, but error message is different.)
The problem is already with the expression a(i), such indexing is not possible:
>> a(i)
error: subscript indices must be integers or boolean
In a situation where a are the integer indices, you are probably better off using prod:
>> prod(a)
ans = (sym) a₁₁⋅a₁₂⋅a₁₃
An alternative is to work with functions:
>> syms fa(i)
>> e=x0*symprod(fa(i), i, [1 3])
e = (sym) x₀⋅fa(1)⋅fa(2)⋅fa(3)

Octave - System of differential equations with lsode

here is my problem. I'm trying to solve a system of two differential equations thanks to the two functions below. The part of the code that give me some trouble is the variable "rho". "rho" is a function which values are given from a file and that I tried to put in the the variable rho.
function [xdot]=f2(x,t)
# Parameters of the equations
t=[1:1:35926];
x = dlmread('data_txt.txt');
rho=x(:,4);
beta = 0.68*10^-2;
g = 1.5;
l = 1.6;
# Definition of the system of 2 ODE's :
xdot(1) = ((rho-beta)/g)*x(1) + l*x(2);
xdot(2) = (beta/g)*x(1)-l*x(2);
endfunction
.
# Initial conditions for the two variables :
x0 = [0;1];
# Definition of the time-vector -> (initial time,temporal mesh,final time) :
t = linspace (1, 10, 10000);
# Resolution with the lsode routine :
x = lsode ("f2", x0, t);
# Plot of the two curves :
plot (t,x);
When I run my code, I get the following error:
>> resolution_effective2
error: f2: A(I) = X: X must have the same size as I
error: called from
f2 at line 34 column 9
resolution_effective2 at line 8 column 3
error: lsode: evaluation of user-supplied function failed
error: called from
resolution_effective2 at line 8 column 3
error: lsode: inconsistent sizes for state and derivative vectors
error: called from
resolution_effective2 at line 8 column 3
I know that my error comes from a mismatch of size between some variable but I have been looking for the error for days now and I don't see. Could someone try to give and explain me an effective correction ?
Thank you
The error might come from your function f2. Its first argument x should have the same dimension as x0 since x0 is the initial condition of x.
In your case, whatever you intent to be the first argument of f2 is ignored since in your function you do x = dlmread('data_txt.txt'); this seems to be a mistake.
Then, xdot(1) = ((rho-beta)/g)*x(1) + l*x(2); will be a problem since rho is a vector.
You need to check the dimensions of x and rho.

Can Matrices be passed as arguments in Octave?

I am implementing the non-vectorized form of the cost function in octave. This is the code from my .m file
function computeCost(X, y, theta)
sigma=0;
theta0 = 0;
m = length(y);
for i = 1:m
sigma = sigma+ theta*X(i)-y(i);
end;
J = ((sigma)^2)/2*m;
end;
My octave code is:
>> X= [1,1; 1,2; 1,3; 1,4;];
>> y= [2;4;6;8];
>> J = computeCost(X, y, 0.5);
where X and y are matrices. However, I am getting this output on my CLI Window:
Error: computeCost(X, y, 0.5) undefined near line 1, column 5
I've checked my code, there is no apparent issue. Is it because Octave does not accept matrices as parameters for its functions?
The answer to your question is clearly YES: The name MATLAB is an abbreviation of Matrix laboratory. Octave and Matlab are specially designed to facilitate working with matrices.
The problem in your code is: Your function definition is incomplete. You have not defined J as return value. The error message you see is a bit missleading because it should state column 10 as place of the error. When you change the first line of your code to
function J = computeCost(X, y, theta)
It will work as expected and output the value 648.

How do I assign variables in matrices?

I can't make matrices with variables in it for some reason. I get following message.
>>> A= [a b ;(-1-a) (1-b); (1+a) b]
error: horizontal dimensions mismatch (2x3 vs 1x1)
Why is it? Please show me correct way if I'm wrong.
In Matlab you first need to assign a variable before you can use it,
a = 1;
b = a+1;
This will thus give an error,
clear;
b = a+1; % ERROR! Undefined function or variable 'a
Matlab does never accept unassigned variables. This is because, on the lowest level, you do not have a. You will have machine code which is assgined the value of a. This is handled by the JIT compiler in Matlab, so you do not need to worry about this though.
If you want to use something as the variable which you have in maths you can specifically express this to matlab. The object is called a sym and the syntax that define the sym x to a variable xis,
syms x;
That said, you can define a vector or a matrix as,
syms a b x y; % Assign the syms
A = [x y]; % Vector
B = A= [a b ;(-1-a) (1-b); (1+a) b]; % Matrix.
The size of a matrix can be found with size(M) or for dim n size(M,n). You can calcuate the matrix product M3=M1*M2 if and only if M1 have the size m * n and M2 have the size n * p. The size of M3 will then be m * p. This will also mean that the operation A^N = A * A * ... is only allowed when m=n so to say, the matrix is square. This can be verified in matlab by the comparison,
syms a b
A = [a,1;56,b]
if size(A,1) == size(A,2)
disp(['A is a square matrix of size ', num2str(size(A,1)]);
else
disp('A is not square');
end
These are the basic rules for assigning variables in Matlab as well as for matrix multiplication. Further, a google search on the error error: 'x' undefined does only give me octave hits. Are you using octave? In that case I cannot guarantee that you can use sym objects or that the syntaxes are correct.