Please explain this Octave Error Code: op1 is 1x12, op2 is 1x3 - octave

Code:
function result = assess_polynomial_n(y,n)
[coeff, t, pred_accum] = fit_data(y, n);
[avg, std_dev] = compute_error(coeff,y);
[coeff1, t, pred_accum1] = fit_data(y, n+1);
[avg1, std_dev1] = compute_error(coeff1,y);
if avg1>avg
result = n;
elseif avg<0.1 && std_dev<0.1
result = n;
elseif pred_accum1.*(1)<0.0001
result = n;
else
result = 0;
end
end
***Error***
error: compute_error: operator -: nonconformant arguments (op1 is 1x12, op2 is 1x3)
error: called from
compute_error at line 11 column 17
assess_polynomial_n at line 19 column 18
__tester__.octave at line 38 column 1
Does anyone know why I could be getting this error? We're supposed to be working in MATLAB but pasting our code into Moodle which uses Octave if that clears anything up.
I also will post the functions I'm calling for reference since the error is occurring in Compute_Error. This is a part of an overall project where I was to define the functions I'm calling before creating assess_polynomial_n.
function [coeff, t, pred_accum] = fit_data(y, n)
len = length(y);
t = 10*[0:len-1];
coeff = polyfit(t, y, n);
pred_accum = polyval(coeff, t);
end
function [avg std_dev] = compute_error(x,y)
s=[];
for i=1:length(x)
error=abs(x(i)-y(i));
s=[s,error];
end
avg = mean(s);
std_dev = std(s);
end

The problem lies in this line of code:
[avg1, std_dev1] = compute_error(coeff1,y);
You are trying to compute the error between the original data and the coefficients of the fitted polynomial. It doesn't make sense, right?
Instead, you should compute the error between the original data and the fitted data, which is the polynomial's output at each t value. The correct code would be:
[avg1, std_dev1] = compute_error(pred_accum1,y);

Related

Why do I get the error "Array indices must be positive integers or logical values" in MATLAB

I need to write the function among others in MATLAB, but I keep getting "Array indices must be positive integers or logical values." error and an error for both functions f(x) and u(x) saying "Error in f(x(j)) = ..." or "Error in u(x(j)) = ....." Why is that?
This is the code relating to that
for j=1:n
x(j)=-1+j*h;
f(x(j)) = (pi.^2).*(exp(cos(pi.*x(j)))).*(cos(pi.*x(j))-(sin(pi.*x(j))).^2);
u(x(j))= -f(x(j));
end
The error coming from the value of h must be integer for example:
if h = 1; f(1), u(1) no error
if h = 1.3; f(1.3), u(1.3) error
The is no reserved place in array f(1.3) by 1.3;
Fix h value by an integer to solve the error.
n = 99;
h = 2;
x = ones(n,1);
f = ones(n,1);
u = ones(n,1);
for j=1:n
x(j) = -1+j*h;
f(x(j)) = (pi.^2).*(exp(cos(pi.*x(j)))).*(cos(pi.*x(j))-(sin(pi.*x(j))).^2);
u(x(j)) = -f(x(j));
end

Please specify the error in this octave code

So I am trying to solve this problem from coursera regarding onevsAll classification. It comes under the ml course under Andrew NG. I am not able to find mistake in my code and it keeps showing this error
error: fmincg: operator +: nonconformant arguments (op1 is 401x1, op2 is 4x1)
error: called from
fmincg at line 87 column 5
oneVsAll at line 60 column 13
ex3 at line 77 column 13
I have tried reading the fmincg code but I cannot understand the problem.
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
for c = 1:num_labels
initial_theta = zeros(n+1,1) ;
options = optimset('GradObj', 'on', 'MaxIter', 50);
[theta] = fmincg (#(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);
all_theta(c, :) = theta;
end;
Program is showing error mentioned above regarding the fmincg file.

How to pass variadic arguments in Octave

I would like to implement a function duration = timer(n, f, arguments_of_f) that would measure how much time does a method f with arguments arguments_of_f need to run n times. My attempt was the following:
function duration = timer(n, f, arguments_of_f)
duration = 0;
for i=1:n
t0 = cputime;
f(arguments_of_f);
t1 = cputime;
duration += t1 - t0;
end
In another file, I have
function y = f(x)
y = x + 1;
end
The call d1 = timer(100, #f, 3); works as expected.
In another file, I have
function y = g(x1, x2)
y = x1 + x2;
end
but the call d2 = timer(100, #g, 1, 2); gives an error about undefined
argument x2, which is, when I look back, somehow expected, since I pass only
1 to g and 2 is never used.
So, how to implement the function timer in Octave, so that the call like
timer(4, #g, x1, ... , xK) would work? How can one pack the xs together?
So, I am looking for the analogue of Pythons *args trick:
def use_f(f, *args):
f(*args)
works if we define def f(x, y): return x + y and call use_f(f, 3, 4).
You don't need to pack all the arguments together, you just need to tell Octave that there is more than one argument coming and that they are all necessary. This is very easy to do using variadic arguments.
Your original implementation is nearly spot on: the necessary change is minimal. You need to change the variable arguments_to_f to the special name varargin, which is a magical cell array containing all your arbitrary undeclared arguments, and pass it with expansion instead of directly:
function duration = timer(n, f, varargin)
duration = 0;
for i=1:n
t0 = cputime;
f(varargin{:});
t1 = cputime;
duration += t1 - t0;
end
That's it. None of the other functions need to change.

Solving two non-linear equations in Octave

I am trying to solve the following two equations using Octave:
eqn1 = (wp/Cwc)^(2*N) - (1/10^(0.1*Ap))-1 == 0;
eqn2 = (ws/Cwc)^(2*N) - (1/10^(0.1*As))-1 == 0;
I used the following code:
syms Cwc N
eqn1 = (wp/Cwc)^(2*N) - (1/10^(0.1*Ap))-1 == 0;
eqn2 = (ws/Cwc)^(2*N) - (1/10^(0.1*As))-1 == 0;
sol = solve(eqn1 ,eqn2, Cwc, N)
ws,wp,As, and Ap are given as 1.5708, 0.31416, 0.5, 45 respectively.
But I am getting the following error:
error: Python exception: NotImplementedError: could not solve
126491*(pi*(3*10**N*sqrt(314311)*pi**(-N)/1223)**(1/N)/2)**(2*N) - 126495
occurred at line 7 of the Python code block:
d = sp.solve(eqs, *symbols, dict=True)
What should I do to solve this?
Edit:
I modified the equations a little bit.
pkg load symbolic
clear all
syms Cwc N
wp = 0.31416
ws = 1.5708
As = 45
Ap = 0.5
eqn2 = N - log10(((1/(10^(0.05*As)))^2)-1)/2*log10(ws/Cwc) == 0;
eqn1 = N - log10(((1/(10^(0.05*Ap)))^2)-1)/2*log10(wp/Cwc) == 0;
sol = solve(eqn1,eqn2,Cwc,N)
And now I am getting this error:
error: Python exception: AttributeError: MutableDenseMatrix has no attribute is_Relational.
occurred at line 3 of the Python code block:
if arg.is_Relational:
Looking at the equations, with unknowns in the base and exponent of the same term, highly suggests there is no symbolic solution to be found. I gave a simplified system (2/x)^y = 4, (3/x)^y = 5 to a couple of symbolic solvers, neither of which got anything from it. So, the only way to solve this is numerically (which makes sense because the four known parameters you have are some floating point numbers anyway). Octave's numeric solver for this purpose is fsolve. Example of usage:
function y = f(x)
Cwc = x(1);
N = x(2);
ws = 1.5708;
wp = 0.31416;
As = 0.5;
Ap = 45;
y = [(wp/Cwc)^(2*N) - (1/10^(0.1*Ap))-1; (ws/Cwc)^(2*N) - (1/10^(0.1*As))-1];
endfunction
fsolve(#f, [1; 1])
(Here, [1; 1] is an initial guess.) The output is
0.31413
0.19796

Solving system of ODEs using Octave

I am trying to solve a system of two ODEs using Octave, and in particular the function lsode.
The code is the following:
function xdot = f (x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
t = linspace(0, 5000, 200)';
x0 = [1000; 1000];
x = lsode ("f", x0, t);
set term dumb;
plot(t,x);
I am getting continuously the same error, that "x" is not defined, and I do not know why. The error is the following:
warning: function name 'f' does not agree with function file name '/home /Simulation 1/sim.m'
error: 'x' undefined near line 17 column 17
error: called from
sim at line 17 column 9
It would we great that any of you could help me with this code.
You have two errors. One, you are not saving your source code with the proper name. Two, variable "x" is a vector, and nothing in your script indicates that. You should add a line "x = zeros(1,2);" right after "xdot = zeros(2,1);".
Try the following code:
function ODEs
t = linspace(0, 5000, 200);
x0 = [1000; 1000];
x = lsode (#f, x0, t);
fprintf('t = %e \t\t x = %e\n',t,x);
endfunction
function xdot = f(x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
x = zeros(1,2);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
Save it as ODEs.m and execute it. It does not plot anything, but gives you an output with the results for the t range you supplied.