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...
Related
I am attempting to write a function which expands another into a Fourier series. However for some functions the integral() function keeps spitting out warnings claiming it has reached minimum step size which is likely due to a singularity at x = -1. My code is as follows:
H = #(t) 1 * (t >= 0) + 0; % Heaviside step function
x_a = #(t) 2*(H(mod(t+1, 4)) - H(mod(t+1, 4) - 2)) - 1;
time = linspace(-8, 8, 25);
plot(time, x_a(time))
ylim([-1.5 1.5])
xlim([-8 8])
% This is where it starts spitting out warnings if the next line is uncommented
%x_a_fourier = fourier(x_a, time, 4, 10);
function x = fourier(F, I, T, m)
a_0 = (1/T) * integral(#(x) F(x), -T/2, T/2);
x = a_0 * ones(1, length(I));
w_0 = (2*pi) / T;
a_n = #(n) (2/T) * integral(#(x) F(x) .* cos(n*w_0*x), -T/2, T/2);
b_n = #(n) (2/T) * integral(#(x) F(x) .* sin(n*w_0*x), -T/2, T/2);
for k = 1:length(I)
for l = 1:m
x(k) = x(k) + a_n(l) * cos(l*w_0*I(k)) + b_n(l) * sin(l*w_0*I(k));
end
end
end
From the plot() statement it should be obvious that the integral() function shouldn't run into any singularities. Any ideas as to what may be the problem?
I try to solve a problem with 3 features and 6 classes(label). The training dataset is 700 rows * 3 columns. The features values are continious from 0-100. I use one-Vs-all method, but I do not know why the prediction accuracy is so small, just 24%. Could anyone tell me, please? Thank you!
This is how I do the prediction:
function p = predictOneVsAll(all_theta, X)
m = size(X, 1);
num_labels = size(all_theta, 1);
% You need to return the following variables correctly
p = zeros(size(X, 1), 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
[m, p] = max(sigmoid(X * all_theta'), [], 2);
end
And the One-Vs-all
% 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];
initial_theta = zeros(n+1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 20);
for c = 1:num_labels,
[theta] = ...
fmincg (#(t)(lrCostFunction(t, X, (y == c), lambda)), ...
initial_theta, options);
all_theta(c,:) = theta';
end
In predictOneVsAll , you don't need to use sigmoid function. You need it only when the calculating cost. So correct code is,
[m, p] = max((X * all_theta'), [], 2);
In OneVsAll , loop should look like this
for c = 1:num_labels
all_theta(c,:) = fmincg (#(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
endfor
It is better if you ask these questions in andrew's ML course discussion. They would be more familiar with the code and the problem.
This is my first attempt to write anything in matlab, so please, be patient.
I am trying to evaluate the solution of the following ODE: w'' + N(w, w') = f(t) with the Cauchy conditions w(0) = w'(0) = 0. Here N is a given nonlinear function, f is a given source. I also need the function
where G is the solution of the following ODE:
where G(0) = G'(0) =0, s is a constant, and
My try is as follows: I define N, f, w and G:
k = 1000;
N = #(g1,g2) g1^2 + sin(g2);
f = #(t) 0.5 * (1 + tanh(k * t));
t = linspace(0, 10, 100);
w = nonlinearnonhom(N, f);
G = nonlinearGreen(N);
This part is ok. I can plot both w and G: both seems to be correct. Now, I want to evaluate wG. For that purpose, I use the direct and inverse Laplace transforms as follows:
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
but is says
Undefined function 'laplace' for input arguments of type 'double'.
Error in main (line 13)
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
Now, I am not sure if this definition of wG is correct at all and if there are not any other definitions.
Appendix: nonlinearGreen(N) is defined as follows:
function G = nonlinearGreen(N)
eps = .0001;
del = #(t)[1/(eps * pi) * exp( -t^2/eps^2)];
eqGreen = #(t, g)[g(2); - N(g(1),g(2)) + del(t)];
tspan = [0, 100];
Cc = [0, 0];
solGreen = ode45(eqGreen, tspan, Cc);
t = linspace(0, 10, 1000);
G = deval(solGreen, t, 1);
end
and nonlinearnonhom is defined as follows:
function w = nonlinearnonhom(N, f)
eqnonhom = #(t, g)[g(2); - N(g(1),g(2)) + f(t)];
tspan = [0, 100];
Cc = [0, 0];
solnonhom = ode45(eqnonhom, tspan, Cc);
t = linspace(0, 10, 100);
w = deval(solnonhom, t, 1);
end
You keep mixing different kind of types and it's not a good idea. I suggest you keep with symbolic all the way if you want to use the laplace function. When you define N and f with #(arobase) as function handles and not symbolic expressions as you might want to do. I suggest you have a look at symbolic documentation and rewrite your functions as symbolic.
Then, the error message is pretty clear.
Undefined function 'laplace' for input arguments of type 'double'.
Error in main (line 13)
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
It means that the function laplace can't have arguments of type double.
The problem is that your t is a vector of double. Another mistake is that s is not defined in your code.
According to Matlab documentation of laplace, all arguments are of type symbolic.
You can try to manually specify symbolic s and t.
% t = linspace(0, 10, 100); % This is wrong
syms s t
wG = ilaplace(laplace(G, t, s) * laplace(f, t, s), s, t);
I have no error after that.
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
I have the following homework question:
Apply linear least squares with the two models S1(A, B, C) = Ax^2 + Bx
+ C and S2(A, B, C, D) = Ax^3 + Bx^2 + Cx + D to the data set (0, 4), (1, −1), (2, 6), (3, 1), (4, −4), (5, −9). Solve in MATLAB using
lspoly. Report the values of the parameters A, B, C and D clearly, and
produce a plot showing the data and both fitting curves.
I am working with the following function in MATLAB:
Function 1
function y = horner(a,c)
n=length(a)-1;
y=a(n+1);
for k = n:-1:1
y = a(k)+ c*y;
end
Function 2
function C = lspoly(x,y,M)
n = length(x);
F = zeros(n,M+1);
for k = 1:M+1
F(:,k) = x'.^(k-1);
end
A = F'*F;
B = F'*y';
C = A\B;
And this is the code I wrote for the problem:
clc
clear all
close all
x = [0, 1, 2, 3, 4, 5];
y = [4, -1, 6, 1, -4, -9];
C = lspoly(x,y,2); % finds the parameters
xx = 0:0.01:5;
yy = zeros(1, length(xx));
for i=1:length(xx)
yy(i) = horner(C,xx(i));
end
CC = lspoly(x,y,3); % finds the parameters
xxx = 0:0.1:5;
yyy = zeros(1, length(xxx));
for i=1:length(xxx)
yyy(i) = horner(CC,xxx(i));
end
figure(1)
plot(x, y, 'o', xx, yy, 'r-', xxx, yyy, 'b-')
I am encountering some issues with this code. When I try to run the program, I get the following error:
In an assignment A(I) = B, the number of elements in B and I must be
the same.
Error in HW7_1 (line 14) yy(i) = horner(C,xx(i));
I can't really wrap my head around what exactly I need to do to fix this issue. I tried breaking down my program piece by piece to determine results at different spots in the code, but so far nothing of note has been found.
Can someone help me fix this error?
It works just fine for me in Octave 3.8. The only thing I can think of is there a built-in function called horner in MATLAB (part of the Symbolic Math Toolbox), so maybe your code is calling that function instead of yours. Maybe try renaming it to something different, like my_horner or similar.