Matlab Programming : plotting susceptance vs firing angle alpha - matlab

I'm not that strong in programming. I was trying to plot two equations from Power Electronics in Matlab. My code is as follows. I am getting a blank plot when I plot Btcr and alpha! Kindly let me know whats wrong.
Plotting Susceptance vs Firing Angle
%Variables
V = 1;
L = 0.005;
freq = 60;
omega = 2 * pi * freq;
theta = 0:0.01:360*2;
for alpha = 90:10:180
sigma = 2*(180 - alpha)
Btcr = (sigma + sind(2*alpha))/(omega*L*3.14);
end
plot(Btcr, alpha);

Your for-loop is unnecessary. You are actually overwriting the previous values of sigma and Btcr each time you go through the loop, so at the end you don't have vectors of data, just two numbers. You can fix this by removing the loop altogether:
alpha = 90:10:180
sigma = 2*(180 - alpha)
Btcr = (sigma + sind(2*alpha))/(omega*L*3.14);
plot(Btcr, alpha);

Related

Gradient descent and normal equation not giving the same results, why?

I am working on a simple script that tries to find values for my hypothesis. I am using for one a gradient descent and the second the normal equation. The normal equation is giving me the proper results, but my gradient descent not. I can't figure it out with such a simple case why is not working.
Hi, I am trying to understand why my gradient descend does not match the normal equation on linear regression. I am using matlab to implement both. Here's what I tried:
So I created a dummy training set as such:
x = {1 2 3}, y = {2 3 4}
so my hypothesis should converge to the theta = {1 1} so I get a simple
h(x) = 1 + x;
Here's the test code comparing normal equation and gradient descent:
clear;
disp("gradient descend");
X = [1; 2; 3];
y = [2; 3; 4];
theta = [0 0];
num_iters = 10;
alpha = 0.3;
thetaOut = gradientDescent(X, y, theta, 0.3, 10); % GD -> does not work, why?
disp(thetaOut);
clear;
disp("normal equation");
X = [1 1; 1 2; 1 3];
y = [2;3;4];
Xt = transpose(X);
theta = pinv(Xt*X)*Xt*y; % normal equation -> works!
disp(theta);
And here is the inner loop of the gradient descent:
samples = length(y);
for epoch = 1:iterations
hipoth = X * theta;
factor = alpha * (1/samples);
theta = theta - factor * ((hipoth - y)' * X )';
%disp(epoch);
end
and the output after 10 iterations:
gradient descend = 1.4284 1.4284 - > wrong
normal equation = 1.0000 1.0000 -> correct
does not make sense, it should converge to 1,1.
any ideas? Do I have matlab syntax problem?
thank you!
Gradient Descend can solve a lot of different problems. You want to do a linear regression, i.e. find a linear function h(x) = theta_1 * X + theta_2 that best fits your data:
h(X) = Y + error
What the "best" fit is, is debatable. The most common way to define best fit is to minimize the square of the errors between fit and actual data. Assuming that is what you want ...
Replace the function with
function [theta] = gradientDescent(X, Y, theta, alpha, num_iters)
n = length(Y);
for epoch = 1:num_iters
Y_pred = theta(1)*X + theta(2);
D_t1 = (-2/n) * X' * (Y - Y_pred);
D_t2 = (-2/n) * sum(Y - Y_pred);
theta(1) = theta(1) - alpha * D_t1;
theta(2) = theta(2) - alpha * D_t2;
end
end
and change your parameters a bit, e.g.
num_iters = 10000;
alpha = 0.05;
you get the correct answer. I took the code snippet from here which might also provide a nice starting point to read up on what is actually happening here.
Your gradient descend is solving a different thing than the normal equation, you are not inputing the same data. On top of that you seem to overcomplicate a but the theta update, but that is not a problem. Minor changes in your code result in proper output:
function theta=gradientDescent(X,y,theta,alpha,iterations)
samples = length(y);
for epoch = 1:iterations
hipoth = X * theta;
factor = alpha * (1/samples);
theta = theta - factor * X'*(hipoth - y);
%disp(epoch);
end
end
and the main code:
clear;
X = [1 1; 1 2; 1 3];
y = [2;3;4];
theta = [0 0];
num_iters = 10;
alpha = 0.3;
thetaOut = gradientDescent(X, y, theta', 0.3, 600); % Iterate a bit more, you impatient person!
theta = pinv(X.'*X)*X.'*y; % normal equation -> works!
disp("gradient descend");
disp(thetaOut);
disp("normal equation");
disp(theta);

Gradient Descent Overshooting and Cost Blowing Up when used for Regularized Logistic Regression

I'm using MATLAB to code Regularized Logistic Regression and am using Gradient Descent to discover the parameters. All is based on Andrew Ng's Coursera Machine Learning course. I am trying to code the cost function from Andrew's notes/videos. I am not entirely sure if I'm doing it right.
The main problem is... if the number of iterations gets too large, my cost seems to be blowing up. This happens regardless of whether I normalize or not (converting all the data to be between 0 and 1). This problem also causes the decision boundary being produced to shrink (underfit?). Below are three sample results that were obtained, where the decision boundaries of GD are compared against that of Matlab's fminunc.
As can be seen, the cost shoots up when the number of iterations increases. Could it be that I incorrectly coded the cost? Or is there indeed a possibility that Gradient Descent can overshoot? If it helps, I am providing my code. The code I used to calculate the cost history is:
costHistory(i) = (-1 * ( (1/m) * y'*log(h_x) + (1-y)'*log(1-h_x))) + ( (lambda/(2*m)) * sum(theta(2:end).^2) );, based on the equation below:
The full code is given below. Note that I have called other functions as well in this code. Would appreciate any pointers! :) Thank you in advance!
% REGULARIZED Logistic Regression with Gradient Descent
clc; clear all; close all;
dataset = load('ex2data2.txt');
x = dataset(:,1:end-1); y = dataset(:,end); m = length(y);
% Mapping the features (includes adding the intercept term)
x = mapFeature(x(:,1), x(:,2)); % Change to polynomial of the 6th degree
% Define the initial thetas. Same as the number of features, including
% the newly added intercept term (1s)
theta = zeros(size(x,2),1) + 0.05;
initial_theta = theta; % will be used later...
% Set lambda equals to 1
lambda = 1;
% calculate theta transpose x and also the hypothesis h_x
alpha = 0.005;
itr = 120000; % number of iterations set to 120K
for i = 1:itr
ttrx = x * theta; % theta transpose x
h_x = 1 ./ (1 + exp(-ttrx)); % sigmoid hypothesis
error = h_x - y;
% the gradient a.k.a. the derivative of J(\theta)
for j = 1:length(theta)
if j == 1
gradientA(j,1) = 1/m * (error)' * x(:,j);
theta(j) = theta(j) - alpha * gradientA(j,1);
else
gradientA(j,1) = (1/m * (error)' * x(:,j)) - (lambda/m)*theta(j);
theta(j) = theta(j) - alpha * gradientA(j,1);
end
end
costHistory(i) = (-1 * ( (1/m) * y'*log(h_x) + (1-y)'*log(1-h_x))) + ( (lambda/(2*m)) * sum(theta(2:end).^2) );
end
[cost, grad] = costFunctionReg(initial_theta, x, y, lambda);
% Using MATLAB's built-in function fminunc to minimze the cost function
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 500);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[thetafm, cost] = fminunc(#(t)(costFunctionReg(t, x, y, lambda)), initial_theta, options);
close all;
plotDecisionBoundary_git(theta, x, y); % based on GD
plotDecisionBoundary_git(thetafm, x, y); % based on fminunc
figure;
plot(1:itr, costHistory(:), '--r');
title('The cost history based on GD');

Spiral of non-overlapping circles

I want to create a spiral of circle markers which never overlap with each other. This is what I got so far, but it overlaps the first markers, and the last ones are too far apart from each other.
t = pi : pi/20 : 20*pi;
t = asind(1./t);
r = t;
x = r .* cos(t);
y = r .* sin(t);
plot(x,y,'o-');
axis equal; hold on
Plotting without redefining t as asinf(1/t) as follows, is shown in the second plot.
t = pi : pi/20 : 20*pi;
r = t;
x = r .* cos(t);
y = r .* sin(t);
plot(x,y,'o-');
Any ideas on how does the spacing of the angles t must be to accomplish that the markers don't overlap?
You can approximate the arc length, greatly simplifying Gilles-Phillipe's solution. This is a simplification, which means that the distance between the markers is not identical everywhere. However the distances are fairly consistent, especially further out.
The approximation here is to assume that the spiral is, locally, a circle. The arc length then is r*dt at a position in the spiral a distance r from the origin, for a change in angle of dt radian.
We now no longer need to solve symbolic equations. I wrote the code in a loop. I'm sure it's possible to vectorize it, making the whole thing two lines of code, but I'll leave that as an exercise to the reader.
This is the code:
d = 1; % step size
q = 1/(2*pi); % spiral constant -- radius grows by q every 1 radian turn
N = 300; % number of points
t = 0; % initial angle
r = d; % initial radius
p = zeros(100,2);
p(1,:) = [r*cos(t),r*sin(t)]; % first point
for ii=2:N
dt = d/r;
t = t+dt;
r = r+dt*q;
p(ii,:) = [r*cos(t),r*sin(t)];
end
clf
plot(p(:,1),p(:,2),'o-')
axis equal
Try this:
syms s;
scale = 10;
l = scale/2 : scale/2 : 40*scale;
t = double(arrayfun(#(y) vpasolve((0.5*(s*sqrt(1+s^2)+asinh(s)))==y,s), l));
x = t .* cos(t);
y = t .* sin(t);
plot(x,y,'o-');
pbaspect([1 1 1]);
axis(scale*[-5 5 -5 5])
The idea is to parameterize using the arclength of the curve. The arclength of this spiral is l=1/2*(t*sqrt(1+t*t)+asinh(t)) (can be found using Matlab symbolic integration). To place points uniformly, we do a uniform sampling of the arclength, and find the corresponding t by solving the equation. Since it cannot be solved easily symbolically, we use a numerical solver.
Note that the scale and the aspect ratio of the plot is really important for it to look uniform and non-overlapping. This is why I added axis/ratio definition. Since each point is solved numerically, it can take quite some time to evaluate. There may be a faster way to do it, but at least you have a result.
I obtain the following result:

Matlab 3dplot function to plot magnitude of a sinusoid varying frequency and time

I'm trying to create a 3d plot to show the amplitude of adding 3 sinusoids of varying phase. The three axes would be: time, frequency and magnitude. I'm just not sure how I can convert what I have into something that the function 3dplot can use. New to this, thanks!
clear;
clc;
inc = 100;
i = 1;
i2 = 1;
for t = 0:(2.413E-9)/inc:2.413E-9 %time range to view signals
for f = 56E9:(64E9-56E9)/inc:64E9 %frequencies from 56ghz - 64ghz
w = 2*pi*f;
wave1 = 0.5*sin(w.*t + (0.25*2));%sinusoids of varying distances, arbitrary amplitude
wave2 = 0.5*sin(w.*t);
wave3 = 0.5*sin(w.*t - (0.25*2));
mag = wave1 + wave2 + wave3;%combining waves
combined(i,i2,i) = mag;
%f,time,magnitude
i = i + 1;%changing frequency index
end
i = 1;
i2 = i2 + 1;%changing time index
end
EDIT: Thanks everyone, I think I have what I was looking for.
Here are some suggestions:
You probably want to replace combined(i,i2,i) = mag; by combined(i,i2) = mag;. That will create combined as a matrix (2D array) giving magnitude as a function of time and frequency
To plot that, use something like surf or imagesc.
The code can be vectorized. This makes it more compact, arguably more readable, and faster.
The figure changes significantly if you change inc. This may be a sign that the current inc is causing aliasing in the representation. You may want to increase that parameter.
You may want to avoid using i as variable name.
Code:
clear;
clc;
inc = 100;
t = 0:(2.413E-9)/inc:2.413E-9; %time range to view signals
f = 56E9:(64E9-56E9)/inc:64E9 %frequencies from 56ghz - 64ghz
w = 2*pi*f;
wave1 = 0.5*sin(bsxfun(#times, w.', t) + (0.25*2));%sinusoidsdistances
wave2 = 0.5*sin(bsxfun(#times, w.', t));
wave3 = 0.5*sin(bsxfun(#times, w.', t) - (0.25*2));
combined = wave1 + wave2 + wave3;%combining waves
%f,time,magnitude
surf(t, f, combined, 'edgecolor', 'none')
xlabel time
ylabel frequency

Plotting wind speed and wind direction over time

I'm trying to make a plot that can display both wind speed and wind direction over time. A colleague suggested adding a line pointing in the wind direction to each point on a time-series plot of wind speed.
I thought it would be fairly simple to calculate the line. I used trig formulas from here. However the plot is not displaying as I'd expect. All of the lines look as if they have zero slope rather than vary from -1 to 1.
Here is my code:
wdates = [ 7.357325746759259E5; 7.357325747916667E5; 7.357325749074074E5; 7.357325750231481E5; 7.357325751388889E5; 7.357325752546296E5; 7.357325753703704E5; 7.357325754861111E5; 7.357325756018518E5; 7.357325757175926E5; 7.357325758333333E5; 7.357325759490741E5; 7.357325760648148E5];
topspeed = rand(size(wdates)) * 2;
toprdir = [0 pi/6 pi/4 pi/3 pi/2 2*pi/3 3*pi/4 pi 5*pi/4 4*pi/3 3*pi/2 5*pi/3 7*pi/4];
toprdir = toprdir';
h = figure(1);
plot(wdates,topspeed,'s');
datetick('x')
hold all;
%find slope
topslopes = tan(toprdir);
for i=1:length(wdates)
%find start point.
clear x;
clear y;
x(1) = wdates(i);
y(1) = topspeed(i);
d = .0001;
%x(2) = d * cos(atan(topslopes(i))) + x(1); %did not work?
%y(2) = d * sin(atan(topslopes(i))) + y(1); %did not work?
x(2) = d * cos(toprdir(i)) + x(1);
y(2) = d * sin(toprdir(i)) + y(1);
plot(x,y);
end
And here is the result.
You are seeing that all the lines look like they have a slope of zero because your axes have very different ranges.
Instead, create a scaling factor based on your axes ranges. Note that, I just approximated values for dy, dx, but you should calculate what they should be based on your data and physical dimensions of each of your axis (e.g. make sure a 45 deg line looks like 45 degrees). You can use "axis square" to make dimensions the same.
dy = 1;
dx = 0.001;
x(2) = dx * cos(toprdir(i)) + x(1);
y(2) = dy * sin(toprdir(i)) + y(1);
With these lines modified, the resulting graph looks as follows: