What does this syntax [0:1:5] mean (do) in the context of the given code? - matlab

I don't understand how [0:1:5] is being used in the code below:
function [x , y] = plotTrajectory(Vo,O,t,g)
% calculating x and y values
x = Vo * cos(O) * t ;
y = Vo*(sin(O)*t)-(0.5*g*(t.^2));
plot (x,y);
hold on
end
for i = (0: (pi/8): pi);
[x,y] = plotTrajectory(10,i,[0:1:5],9.8);
end

Each of the parameters are being used to find particular X and Y values. O changes from 0 to pi in steps of pi/8 while Vo, t and g remain unchanged.
The t variable is simply an array from 0 to 5 in steps of 1 and so there are 6 time points defined all together. With these time points and with a particular value of O, but with the values of Vo, t and g being held constant throughout this entire endeavour, 6 X and Y points are defined and are thus plotted on a graph. A graph is generated for each value of O and thus a set of 6 different X and Y points are generated. Each graph with each value of O are all plotted on the same graph.
We can rewrite the above code in pseudo-code to make it easier to understand as follows:
for i = 0, pi/8, 2*pi/8, ..., pi
define Vo = 10
define O = i
define t = [0, 1, 2, 3, 4, 5]
define g = 9.8
run function plotTrajectory(Vo, O, t, g)
end
function plotTrajectory(Vo, O, t, g)
calculate x = Vo * cos(O) * t, for t = 0, 1, 2, 3, 4, 5
calculate y = Vo * (sin(O) * t) - (0.5 * g * t^2), for t = 0, 1, 2, 3, 4, 5
plot x and y for t = 0, 1, 2, 3, 4, 5 on the same graph
end

Related

Issue with symsum in Matlab

MATLAB Result
Above is a photo of the code and the output of the code and I'll also paste it below:
L = 1.5;
W = 1;
x = 0.75;
y = 0.5;
T1 = 50;
T2 = 150;
W = 1;
syms n y x L W
A = (((-1)^(n+1)+1)/n)*(sin(n*pi*x))*((sinh(n*pi*y)/(L))/(sinh((n*pi)/L)));
S1 = symsum(A,n,1,8)
% S2 = symsum(A,n,[1 3 5 7 9])
The S1 value is my test value but ideally, I want to evaluate A where n = 1, 3, 5, 7, 9 like I tried to do with S2. I can't seem to get the variables to work by plugging into A or evaluating A with n. Even when I do get an answer, it's not evaluated to a number like I need it to be... do y'all have any suggestions?

Multidimensional data storage and interpolation

I have a function (so to speak, i actually have data with this characteristic) with one variable x and several parameters a, b and c, so y = f(x, a, b, c).
Now i want to interpolate within families of parameters (for example for variations of a).
I'm currently doing this for data with one parameter (here, y is the data matrix)
% generate variable and data
x = linspace(0, 1, 100);
a = [0, 1]; % parameter
for i = 1:length(a)
y(:, i) = x.^2 + a(i);
end
% interpolate:
yi = interp1(a, y.', 0.5);
This works fine, but how do i expand this to more dimensions?
My current data format is like this: Each column of my data matrix represents one specific set of parameters, so for example:
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
where the first column denotes a = 0, b = 0, the second a = 1, b = 0, the third a = 0, b = 1 and the last a = 1, b = 1 (values are just for clarification, this is not on purpose binary. Also, the data columns are obviously not the same).
This data format is just the consequence of my data aquisition scheme, but i'm happy to change this into something more useful. Whatever works.
Works well for me:
% generate variable and data
x = linspace(0, 1, 100);
a = [0, 1, 2]; % parameter
b = [3, 4, 5]; % parameter
c = [6, 7, 8]; % parameter
% Create grid
[X,A,B,C]=ndgrid(x,a,b,c);
% define function
foo = #(x,p1,p2,p3) p1.*x.^2 + p2.*x + p3;
% evaluate function
Y = foo(X,A,B,C);
% interpolate:
yi = interpn(X,A,B,C,Y,x,1,4,6);
#zlon's answer works fine for the interpolation part, here i want to show how to convert the data from the format i provided to the needed format for the interpolation.
The two-dimensional matrix must be transformed into a N-dimensional one. Since the columns are not necessarily in order, we need to find the right ones. This is what i did:
First, we need to know the parameter set of each column:
a = [ 2, 2, 1, 0, 0, 1 ];
b = [ 1, 0, 0, 1, 0, 1 ];
These vectors length match the number of columns in the data matrix. The first column for example now contains the data for a = 2 and b = 1.
Now we can generate the new table:
A = -Inf;
i = 1;
while true
A = min(a(a > A)); % find next a
if isempty(A)
break
end
idxa = find(a == A); % store possible indices
B = -Inf;
j = 1;
while true
B = min(b(b > B))); % find next b
if isempty(B)
break
end
idxb = find(b == B); % store possible indices
% combine both indices
idx = intersect(idxa, idxb);
% save column in new data table
data(:, i, j) = olddata(:, idx);
% advance
j = j + 1;
end
i = i + 1;
end

nlinfit in MatLab receives an unexpected result

The quiz I met first gave me an Logistic model:
And ask me to linearize it, then evaluate the value of a and k according to the data it gave( in this subject L is took as 3000). I finished that, but got into trouble in the second subject which asked me to do a non-linear-regression with a and k's value evaluated in the first subject. Here's my code:
function y = func(const, t)
y = const(1)./(1 + const(2)*exp(-const(3)*t));
end
t = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
y = [43.65, 109.86, 187.21, 312.67, 496.58, 707.65 , ...
960.25, 1238.75, 1560, 1824.29, 2199, 2438.89, 2737.71];
yAss = log ((3000 ./ y) - 1);
p = polyfit (t, yAss, 1);
a = exp (1) ^ (p(2));
k = -p(1);
beta0 = [3000, a, k];
beta = nlinfit (t, yAss, #func, beta0);
yAfter = beta(1) ./ (1 + beta(2) * exp (-beta(3) * t));
yCompare = 3000 ./ (1 + a * exp (-k * t));
scatter (t, y); hold on;
plot (t, yAfter, 'r');
plot (t, yCompare);
And what it gave:
The red curve is generated with the value returned by nlinfit, Anyone could tell me what is wrong?
I feel stupid... Answering my own question and the question itself is stupid either...
beta = nlinfit (t, yAss, #func, beta0);
should be:
beta = nlinfit (t, y, #func, beta0);
I really want to delete that question...

Solving unknowns of a matrix in Matlab

I have a list of formula stored in a cell array, and I solve the unknowns within the matrix.
For example, consider a 2*2 matrix:
[2x+y, 4q+z; 3x+0.5y, 2q+12z ]
How to solve q,x,y,z by setting each cell equals 20? (i.e., q= 4, x =5, y = 10, z=1)
You're asking to solve a linear system. The canonical way to write a linear system is as A*x = b where A is a matrix, x is the vector to solve for, and b is also a vector. Writing your problem (in math) using matrices, the system is:
[0 2 1 0 [q [20
4 0 0 1 * x = 20
0 3 .5 0 y 20
2 0 0 12] z] 20]
To solve the system numerically in MATLAB:
A = [0, 2, 1, 0; 4, 0, 0, 1;, 0, 3, .5, 0; 2, 0, 0, 12];
b = [20; 20; 20; 20];
xsol = linsolve(A, b);
You could also do xsol = A \ b. A point of caution: both linsolve and \ will solve the system in the least squares sense if the system is overdetermined (typically, system is overdetermined if A is m by n where m > n).
xsol(1) will give the value for q, xsol(2) will give value for x, etc...
Solution is [4.7826; 5.0000; 10.0000; 0.8696]
One way to achieve what you are looking for is to use the Symbolic Toolbox. Here is an example code to solve for q, x, y, and z.
syms q x y z
A = [2*x+y==20, 4*q+z==20; 3*x+0.5*y==20, 2*q+12*z==20];
S = solve(A,[q x y z]);
disp([S.q S.x S.y S.z]);
Output:
[110/23, 5, 10, 20/23]

Finding an equation of linear classifier for two separable sets of points using perceptron learning

I would like to write a matlab function to find an equation of a linear classifier for 2 separable sets of points using one single-layer perceptron. I have got 2 files:
script file - run.m:
x_1 = [3, 3, 2, 4, 5];
y_1 = [3, 4, 5, 2, 2];
x_2 = [6, 7, 5, 9, 8];
y_2 = [3, 3, 4, 2, 5];
target_array = [0 0 0 0 0 1 1 1 1 1];
[ func ] = classify_perceptron([x_1 x_2; y_1 y_2], target_array);
x = -2:10;
y = arrayfun(func, x);
plot(x_1, y_1, 'o', x_2, y_2, 'X', x, y);
axis([-2, 10, -2, 10]);
classify_perceptron.m
function [ func ] = classify_perceptron( points, target )
% points - matrix of x,y coordinates
% target - array of expected results
% func - function handler which appropriately classifies a point
% given by x, y arguments supplied to this function
target_arr = target;
weights = rand(1, 2);
translation = rand();
for i=1:size(points, 2)
flag = true;
while flag
result = weights * points(:, i) + translation;
y = result > 0;
e = target_arr(1, i) - y;
if e ~= 0
weights = weights + (e * points(:, i))';
translation = translation + e;
else
flag = false;
end
end
end
func = #(x)(-(translation + (weights(1, 1) * x)) / weights(1, 2));
return
end
The problem is that I don't know where I am making the mistake that leads to incorrect result. It looks like the slope of the line is right, however translation should be a bit bigger. I would be really thankful for pointing me in the right direction. The result I get is presented in the picture below:
Ok, so I have made a significant progress. In case someone runs into the same problem I present to you the solution. The problem has been solved by adding a variable learning_rate = 0.1 and packing the loop iterating over points into another loop iterating as many times as specified in the variable epochs (e.g. 300) .