there is an error message like "error: parse error near line" every time i try to run this code and i don't know what to do? - octave

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
%theta(iter)=theta(iter)-0.01*(1/m)*(((theta(1)+theta(2))*X-y)*X(iter,2))
theta=theta-(alpha*(1/m)*(X*theta-y)*X(iter,2);
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end

theta=theta-(alpha*(1/m)*(X*theta-y)*X(iter,2);
The parentheses are not balanced as far as I can tell?
You're missing a closing parenthesis ) are you not?

Related

Gradient descent getting wrong linear regression - Octave

I'm trying to make my own project, learning about linear regression.
For some reasone I got wrong linear regression.
I've tried changing alfa values and iterations but it didnt help.
I was checking also for other advices here but I couldnt make it.
GRADIENT DESCENT
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
x = X(:,2);
h = theta(1) + (theta(2) * x);
theta_zero = theta(1) - alpha * (1/m) * sum(h-y); %a-step*gradient(1)
theta_one = theta(2) - alpha * (1/m) * sum((h - y) .* x); %b-step*gardient(2)
theta = [theta_zero; theta_one];
J_history(iter) = computeCost(X, y, theta);
end
end
MAIN CODE
clear ; close all; clc
%% ======================= Part 2: Plotting =======================
fprintf('Plotting Data ...\n')
data = csvread('Fish.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
figure;
plot(X, y, 'dp', 'MarkerSize', 5);
title('Wykres')
fprintf('Program paused. Press enter to continue.\n');
pause;
%% =================== Part 3: Gradient descent ===================
fprintf('Running Gradient Descent ...\n')
X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
%disp(X)
theta = zeros(size(X, 2), 1); % initialize fitting parameters
% Some gradient descent settings
iterations = 1500;
alpha = 0.000001;
% compute and display initial cost
computeCost(X, y, theta)
% run gradient descent
theta = gradientDescent(X, y, theta, alpha, iterations);
% print theta to screen
fprintf('Theta found by gradient descent: ');
fprintf('%f %f \n', theta(1), theta(2));
% Plot the linear fit
hold on; % keep previous plot visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure
FILE
Fish.txt
First column is weight, second is length
242,23.2
290,24
340,23.9
363,26.3
430,26.5
450,26.8
500,26.8
390,27.6
450,27.6
500,28.5
475,28.4
500,28.7
500,29.1
340,29.5
600,29.4
600,29.4
700,30.4
700,30.4
610,30.9
650,31
575,31.3
685,31.4
620,31.5
680,31.8
700,31.9
725,31.8
720,32
714,32.7
850,32.8
1000,33.5
920,35
955,35
925,36.2
975,37.4
950,38
40,12.9
69,16.5
78,17.5
87,18.2
120,18.6
0,19
110,19.1
120,19.4
150,20.4
145,20.5
160,20.5
140,21
160,21.1
169,22
161,22
200,22.1
180,23.6
290,24
272,25
390,29.5
270,23.6
270,24.1
306,25.6
540,28.5
800,33.7
1000,37.3
55,13.5
60,14.3
90,16.3
120,17.5
150,18.4
140,19
170,19
145,19.8
200,21.2
273,23
300,24
5.9,7.5
32,12.5
40,13.8
51.5,15
70,15.7
100,16.2
78,16.8
80,17.2
85,17.8
85,18.2
110,19
115,19
125,19
130,19.3
120,20
120,20
130,20
135,20
110,20
130,20.5
150,20.5
145,20.7
150,21
170,21.5
225,22
145,22
188,22.6
180,23
197,23.5
218,25
300,25.2
260,25.4
265,25.4
250,25.4
250,25.9
300,26.9
320,27.8
514,30.5
556,32
840,32.5
685,34
700,34
700,34.5
690,34.6
900,36.5
650,36.5
820,36.6
850,36.9
900,37
1015,37
820,37.1
1100,39
1000,39.8
1100,40.1
1000,40.2
1000,41.1
200,30
300,31.7
300,32.7
300,34.8
430,35.5
345,36
456,40
510,40
540,40.1
500,42
567,43.2
770,44.8
950,48.3
1250,52
1600,56
1550,56
1650,59
6.7,9.3
7.5,10
7,10.1
9.7,10.4
9.8,10.7
8.7,10.8
10,11.3
9.9,11.3
9.8,11.4
12.2,11.5
13.4,11.7
12.2,12.1
19.7,13.2
19.9,13.8
Thank you in advance!
I tried using standard deviation and Ive replaced
theta = zeros(2, 1);
for
theta = zeros(size(X, 2), 1);
I've also changed values of alpha

Octave: invalid call to a script

I am trying to call to a script, and I am getting an error message. Please help.
The following is what I am trying to run in Octave:
%%% May 23, 2016
%%% Potential Fields for Robot Path Planning
%
%
% Initially proposed for real-time collision avoidance [Khatib 1986].
% Hundreds of papers published on APF
% A potential field is a scalar function over the free space.
% To navigate, the robot applies a force proportional to the
% negated gradient of the potential field.
% A navigation function is an ideal potential field
clc
close all
clear
%% Defining environment variables
startPos = [5,5];
goalPos = [90, 95];
obs1Pos = [50, 50];
obsRad = 10;
goalR = 0.2; % The radius of the goal
goalS = 20; % The spread of attraction of the goal
obsS = 30; % The spread of repulsion of the obstacle
alpha = 0.8; % Strength of attraction
beta = 0.6; % Strength of repulsion
%% Carry out the Potential Field Math as follows:
u = zeros(100, 100);
v = zeros(100, 100);
testu = zeros(100, 100);
testv = zeros(100, 100);
for x = 1:1:100
for y = 1:1:100
[uG, vG] = GoalDelta(x, y, goalPos(1), goalPos(2), goalR, goalS, alpha);
[uO, vO] = ObsDelta(x, y, obs1Pos(2), obs1Pos(1), obsRad, obsS, beta);
xnet = uG + uO;
ynet = vG + vO;
vspeed = sqrt(xnet^2 + ynet^2);
theta = atan2(ynet,xnet);
u(x,y) = vspeed*cos(theta);
v(x,y) = vspeed*sin(theta);
% hold on
end
end
%%
[X,Y] = meshgrid(1:1:100,1:1:100);
figure
quiver(X, Y, u, v, 3)
%% Defining the grid
% Plotting the obstacles
circles(obs1Pos(1),obs1Pos(2),obsRad, 'facecolor','red')
axis square
hold on % Plotting start position
circles(startPos(1),startPos(2),2, 'facecolor','green')
hold on % Plotting goal position
circles(goalPos(1),goalPos(2),2, 'facecolor','yellow')
%% Priting of the path
currentPos = startPos;
x = 0;
while sqrt((goalPos(1)-currentPos(1))^2 + (goalPos(2)-currentPos(2))^2) > 1
tempPos = currentPos + [u(currentPos(1),currentPos(2)), v(currentPos(1),currentPos(2))]
currentPos = round(tempPos)
hold on
plot(currentPos(1),currentPos(2),'-o', 'MarkerFaceColor', 'black')
pause(0.5)
end
The following is the error message I am getting:
error: invalid call to script C:\Users\MyComputer\Downloads\circles.m
error: called from
circles
ECE8743_PotentialFields_Obstacle_1 at line 59 column 1
>>
Here is circles.m:
function [ h ] = circles(x,y,r,varargin)
% h = circles(x,y,r,varargin) plots circles of radius r at points x and y.
% x, y, and r can be scalars or N-D arrays.
%
% Chad Greene, March 2014. Updated August 2014.
% University of Texas Institute for Geophysics.
%
%% Syntax
% circles(x,y,r)
% circles(...,'points',numberOfPoints)
% circles(...,'rotation',degreesRotation)
% circles(...,'ColorProperty',ColorValue)
% circles(...,'LineProperty',LineValue)
% h = circles(...)
%
%% Description
%
% circles(x,y,r) plots circle(s) of radius or radii r centered at points given by
% x and y. Inputs x, y, and r may be any combination of scalar,
% vector, or 2D matrix, but dimensions of all nonscalar inputs must agree.
%
% circles(...,'points',numberOfPoints) allows specification of how many points to use
% for the outline of each circle. Default value is 1000, but this may be
% increased to increase plotting resolution. Or you may specify a small
% number (e.g. 4 to plot a square, 5 to plot a pentagon, etc.).
%
% circles(...,'rotation',degreesRotation) rotates the shape by a given
% degreesRotation, which can be a scalar or a matrix. This is useless for
% circles, but may be desired for polygons with a discernible number of corner points.
%
% circles(...,'ColorProperty',ColorValue) allows declaration of
% 'facecolor' or 'facealpha'
% as name-value pairs. Try declaring any fill property as name-value pairs.
%
% circles(...,'LineProperty',LineValue) allows declaration of 'edgecolor',
% 'linewidth', etc.
%
% h = circles(...) returns the handle(s) h of the plotted object(s).
%
%
%% EXAMPLES:
%
% Example 1:
% circles(5,10,3)
%
% % Example 2:
% x = 2:7;
% y = [5,15,12,25,3,18];
% r = [3 4 5 5 7 3];
% figure
% circles(x,y,r)
%
% % Example 3:
% figure
% circles(1:10,5,2)
%
% % Example 4:
% figure
% circles(5,15,1:5,'facecolor','none')
%
% % Example 5:
% figure
% circles(5,10,3,'facecolor','green')
%
% % Example 6:
% figure
% h = circles(5,10,3,'edgecolor',[.5 .2 .9])
%
% % Example 7:
% lat = repmat((10:-1:1)',1,10);
% lon = repmat(1:10,10,1);
% r = .4;
% figure
% h1 = circles(lon,lat,r,'linewidth',4,'edgecolor','m','facecolor',[.6 .4 .8]);
% hold on;
% h2 = circles(1:.5:10,((1:.5:10).^2)/10,.12,'edgecolor','k','facecolor','none');
% axis equal
%
% % Example 8: Circles have corners
% This script approximates circles with 1000 points. If all those points
% are too complex for your Pentium-II, you can reduce the number of points
% used to make each circle. If 1000 points is not high enough resolution,
% you can increase the number of points. Or if you'd like to draw
% triangles or squares, or pentagons, you can significantly reduce the
% number of points. Let's try drawing a stop sign:
%
% figure
% h = circles(1,1,10,'points',8,'color','red');
% axis equal
% % and we see that our stop sign needs to be rotated a little bit, so we'll
% % delete the one we drew and try again:
% delete(h)
% h = circles(1,1,10,'points',8,'color','red','rot',45/2);
% text(1,1,'STOP','fontname','helvetica CY',...
% 'horizontalalignment','center','fontsize',140,...
% 'color','w','fontweight','bold')
%
% figure
% circles([1 3 5],2,1,'points',4,'rot',[0 45 35])
%
%
% TIPS:
% 1. Include the name-value pair 'facecolor','none' to draw outlines
% (non-filled) circles.
%
% 2. Follow the circles command with axis equal to fix distorted circles.
%
% See also: fill, patch, and scatter.
%% Check inputs:
assert(isnumeric(x),'Input x must be numeric.')
assert(isnumeric(y),'Input y must be numeric.')
assert(isnumeric(r),'Input r must be numeric.')
if ~isscalar(x) && ~isscalar(y)
assert(numel(x)==numel(y),'If neither x nor y is a scalar, their dimensions must match.')
end
if ~isscalar(x) && ~isscalar(r)
assert(numel(x)==numel(r),'If neither x nor r is a scalar, their dimensions must match.')
end
if ~isscalar(r) && ~isscalar(y)
assert(numel(r)==numel(y),'If neither y nor r is a scalar, their dimensions must match.')
end
%% Parse inputs:
% Define number of points per circle:
tmp = strcmpi(varargin,'points')|strcmpi(varargin,'NOP')|strcmpi(varargin,'corners')|...
strncmpi(varargin,'vert',4);
if any(tmp)
NOP = varargin{find(tmp)+1};
tmp(find(tmp)+1)=1;
varargin = varargin(~tmp);
else
NOP = 1000; % 1000 points on periphery by default
end
% Define rotation
tmp = strncmpi(varargin,'rot',3);
if any(tmp)
rotation = varargin{find(tmp)+1};
assert(isnumeric(rotation)==1,'Rotation must be numeric.')
rotation = rotation*pi/180; % converts to radians
tmp(find(tmp)+1)=1;
varargin = varargin(~tmp);
else
rotation = 0; % no rotation by default.
end
% Be forgiving if the user enters "color" instead of "facecolor"
tmp = strcmpi(varargin,'color');
if any(tmp)
varargin{tmp} = 'facecolor';
end
%% Begin operations:
% Make inputs column vectors:
x = x(:);
y = y(:);
r = r(:);
rotation = rotation(:);
% Determine how many circles to plot:
numcircles = max([length(x) length(y) length(r) length(rotation)]);
% Create redundant arrays to make the plotting loop easy:
if length(x)<numcircles
x(1:numcircles) = x;
end
if length(y)<numcircles
y(1:numcircles) = y;
end
if length(r)<numcircles
r(1:numcircles) = r;
end
if length(rotation)<numcircles
rotation(1:numcircles) = rotation;
end
% Define an independent variable for drawing circle(s):
t = 2*pi/NOP*(1:NOP);
% Query original hold state:
holdState = ishold;
hold on;
% Preallocate object handle:
h = NaN(size(x));
% Plot circles singly:
for n = 1:numcircles
h(n) = fill(x(n)+r(n).*cos(t+rotation(n)), y(n)+r(n).*sin(t+rotation(n)),'',varargin{:});
end
% Return to original hold state:
if ~holdState
hold off
end
% Delete object handles if not requested by user:
if nargout==0
clear h
end
end
What am I doing wrong here? How do I correct this error? I am rather new to Octave and Matlab, so any help is greatly appreciated.
this is the first post after pasting the error so maybe it'll help anyone later:
in my case pasting the script into a function worked out!
function retval = name_of_script(parametres)
%your script
endfunction

GNU Octave: 1/N Octave Smoothing of actual FFT Data (not the representation of it)

I would like to smooth an Impulse Response audio file. The FFT of the file shows that it is very spikey. I would like to smooth out the audio file, not just its plot, so that I have a smoother IR file.
I have found a function that shows the FFT plot smoothed out. How could this smoothing be applied to the actual FFT data and not just to the plot of it?
[y,Fs] = audioread('test\test IR.wav');
function x_oct = smoothSpectrum(X,f,Noct)
%SMOOTHSPECTRUM Apply 1/N-octave smoothing to a frequency spectrum
%% Input checking
assert(isvector(X), 'smoothSpectrum:invalidX', 'X must be a vector.');
assert(isvector(f), 'smoothSpectrum:invalidF', 'F must be a vector.');
assert(isscalar(Noct), 'smoothSpectrum:invalidNoct', 'NOCT must be a scalar.');
assert(isreal(X), 'smoothSpectrum:invalidX', 'X must be real.');
assert(all(f>=0), 'smoothSpectrum:invalidF', 'F must contain positive values.');
assert(Noct>=0, 'smoothSpectrum:invalidNoct', 'NOCT must be greater than or equal to 0.');
assert(isequal(size(X),size(f)), 'smoothSpectrum:invalidInput', 'X and F must be the same size.');
%% Smoothing
% calculates a Gaussian function for each frequency, deriving a
% bandwidth for that frequency
x_oct = X; % initial spectrum
if Noct > 0 % don't bother if no smoothing
for i = find(f>0,1,'first'):length(f)
g = gauss_f(f,f(i),Noct);
x_oct(i) = sum(g.*X); % calculate smoothed spectral coefficient
end
% remove undershoot when X is positive
if all(X>=0)
x_oct(x_oct<0) = 0;
end
end
endfunction
function g = gauss_f(f_x,F,Noct)
% GAUSS_F calculate frequency-domain Gaussian with unity gain
%
% G = GAUSS_F(F_X,F,NOCT) calculates a frequency-domain Gaussian function
% for frequencies F_X, with centre frequency F and bandwidth F/NOCT.
sigma = (F/Noct)/pi; % standard deviation
g = exp(-(((f_x-F).^2)./(2.*(sigma^2)))); % Gaussian
g = g./sum(g); % normalise magnitude
endfunction
% take fft
Y = fft(y);
% keep only meaningful frequencies
NFFT = length(y);
if mod(NFFT,2)==0
Nout = (NFFT/2)+1;
else
Nout = (NFFT+1)/2;
end
Y = Y(1:Nout);
f = ((0:Nout-1)'./NFFT).*Fs;
% put into dB
Y = 20*log10(abs(Y)./NFFT);
% smooth
Noct = 12;
Z = smoothSpectrum(Y,f,Noct);
% plot
semilogx(f,Y,'LineWidth',0.7,f,Z,'LineWidth',2.2);
xlim([20,20000])
grid on
PS. I have Octave GNU, so I don't have the functions that are available with Matlab Toolboxes.
Here is the test IR audio file.
I think I found it. Since the FFT of the audio file (which is real numbers) is symmetric, with the same real part on both sides but opposite imaginary part, I thought of doing this:
take the FFT, keep the half of it, and apply the smoothing function without converting the magnitudes to dB
then make a copy of that smoothed FFT, and invert just the imaginary part
combine the two parts so that I have the same symmetric FFT as I had in the beginning, but now it is smoothed
apply inverse FFT to this and take the real part and write it to file.
Here is the code:
[y,Fs] = audioread('test IR.wav');
function x_oct = smoothSpectrum(X,f,Noct)
x_oct = X; % initial spectrum
if Noct > 0 % don't bother if no smoothing
for i = find(f>0,1,'first'):length(f)
g = gauss_f(f,f(i),Noct);
x_oct(i) = sum(g.*X); % calculate smoothed spectral coefficient
end
% remove undershoot when X is positive
if all(X>=0)
x_oct(x_oct<0) = 0;
end
end
endfunction
function g = gauss_f(f_x,F,Noct)
sigma = (F/Noct)/pi; % standard deviation
g = exp(-(((f_x-F).^2)./(2.*(sigma^2)))); % Gaussian
g = g./sum(g); % normalise magnitude
endfunction
% take fft
Y = fft(y);
% keep only meaningful frequencies
NFFT = length(y);
if mod(NFFT,2)==0
Nout = (NFFT/2)+1;
else
Nout = (NFFT+1)/2;
end
Y = Y(1:Nout);
f = ((0:Nout-1)'./NFFT).*Fs;
% smooth
Noct = 12;
Z = smoothSpectrum(Y,f,Noct);
% plot
semilogx(f,Y,'LineWidth',0.7,f,Z,'LineWidth',2.2);
xlim([20,20000])
grid on
#Apply the smoothing to the actual data
Zreal = real(Z); # real part
Zimag_neg = Zreal - Z; # opposite of imaginary part
Zneg = Zreal + Zimag_neg; # will be used for the symmetric Z
# Z + its symmetry with same real part but opposite imaginary part
reconstructed = [Z ; Zneg(end-1:-1:2)];
# Take the real part of the inverse FFT
reconstructed = real(ifft(reconstructed));
#Write to file
audiowrite ('smoothIR.wav', reconstructed, Fs, 'BitsPerSample', 24);
Seems to work! :) It would be nice if someone more knowledgeable could confirm that the thinking and code are good :)

My octave function returns "ans = 0" -Coursera Machine Learning Week 2 problem

I am currently taking Andrew Nguyen's coursera machine learning course and I am on week 2. For a cost function assignment, my function keeps on returning "ans = 0" when it should be returning "ans= 32.07:
My code is below (The variables have been defined in the command window):
%COMPUTECOST Compute cost for linear regression
% J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
% parameter for linear regression to fit the data points in X and y
% Initialize some useful values
% number of training examples
m = length(y);
% You need to return the following variables correctly
J =0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
% You should set J to the cos
h = X * theta;
J = (1/(2m)*(sum(h-y).^2));
% =========================================================================
end

Subscript indices must be real positive integers or logicals

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
% theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by
% taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
% ====================== YOUR CODE HERE ======================
% Instructions: Perform a single gradient step on the parameter vector
% theta.
%
% Hint: While debugging, it can be useful to print out the values
% of the cost function (computeCost) and gradient here.
%
hypothesis = x*theta;
theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
theta_1 = theta(2) - alpha(1/m)*sum((hypothesis-y)*x);
theta(1) = theta_0;
theta(2) = theta_1;
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
I keep getting this error
error: gradientDescent: subscript indices must be either positive integers less than 2^31 or logicals
on this line right in-between the first theta and =
theta_0 = theta(1) - alpha(1/m)*sum((hypothesis-y)*x);
I'm very new to octave so please go easy on me, and
thank you in advance.
This is from the coursera course on Machine Learning from Week 2
99% sure your error is on the line pointed out by topsig, where you have alpha(1/m)
it would help if you gave an example of input values to your function and what you hoped to see as an output, but I'm assuming from your comment
% taking num_iters gradient steps with learning rate alpha
that alpha is a constant, not a function. as such, you have the line alpha(1/m) without any operator in between. octave sees this as you indexing alpha with the value of 1/m.
i.e., if you had an array
x = [3 4 5]
x*(2) = [6 8 10] %% two times each element in the array
x(2) = [4] %% second element in the array
what you did doesn't seem to make sense, as 'm = length(y)' which will output a scalar, so
x = [3 4 5]; m = 3;
x*(1/m) = x*(1/3) = [1 1.3333 1.6666] %% element / 3
x(1/m) = ___error___ %% the 1/3 element in the array makes no sense
note that for certain errors it always indicates that the location of the error is at the assignment operator (the equal sign at the start of the line). if it points there, you usually have to look elsewhere in the line for the actual error. here, it was yelling at you for trying to apply a non-integer subscript (1/m)