Interpolation through Newton polynomials - matlab

I attempted to solve the problem, and would like a solution to compare to.
The question is:
Write a function that determines the (n-1)th order Newton polynomial and interpolates for a
set of values. The inputs of your function should be: a vector of x values, a corresponding
vector of y values, and a vector of values to interpolate. Your outputs should be the
coefficients of the polynomial (as a vector, [a1 a2 ···an]) and the corresponding function
values for the interpolation. Thoroughly comment your code to show that you
know what you are doing.
My attempt is:
function yint = Newtint(x,y,xx)
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
b = zeros(n,n);
b(:,1) = y(:); % the (:) ensures that y is a column vector.
for j = 2:n
for i = 1:n-j+1
b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
xt = 1;
yint = b(1,1);
for j = 1:n-1
xt = xt*(xx-x(j));
yint = yint+b(1,j+1)*xt;
end
% input:
% x = independent variable
% y = dependent variable
% xx = value of independent variable at which
% interpolation is calculated
% output:
% yint = interpolated value of dependent variable
% compute the finite divided differences in the form of a
% difference table

Related

Why is my third MATLAB function outputing only zeros when using ode45?

I need to model negative, positive and simple regulation of a gene for my systems biology class using MATLAB. The problem is that the functions for negative and simple regulation work but the positive regulation function is only outputting zeros.
My script is as follows:
% Simulation of simple regulation, negative autoregulation and positive
% autoregulation
% Define constants
global a b K n
a = 1;
b = 1;
K = 0.5;
n = 2; % Hill coefficient
% Simulation time
tspan = [0,10];
% Initial condition
X0 = 0;
% Run simulations
[t1,X1] = ode45(#autoregulation_f0,tspan,X0); % Simple regulation
[t2,X2] = ode45(#autoregulation_f1,tspan,X0); % Negative autoregulation
[t3,X3] = ode23(#autoregulation_f2,tspan,X0); % Positive autoregulation
% Plot results
figure;
plot(t1,X1,t2,X2,t3,X3);
legend('simple','negative','Location','southeast');
And my functions are:
function dxdt = autoregulation_f0(t,X)
global a b
dxdt = b - a*X;
end
function dxdt = autoregulation_f1(t,X)
global a b K n
dxdt = b/(1+(X^n)/(K^n)) - a*X;
end
function dxdt = autoregulation_f2(t,X)
global a b K n
dxdt = b*X.^n./(K.^n+X.^n) + a*X;
end
The third function "autoregulation_f2(t,X)" is the one that outputs zeros and therefore when plotting the graph I just get a straight line.
Does anyone know what could be causing this?
Thanks in advance!
It looks to be the correct result for the given function. Your provided dxdt has an X in every term. The initial X0=0 will result in dxdt=0, giving you no change in X. As a result you just end up with a flat line.

Taylor series approximation when the series is truncated after the first, second and third term, in MATLAB

I want to write an m-file function demoTaylorlog(x0,dx) that does the equivalent but for the Taylor series representation of f(x) = ln(x).
This is what I have, but it does not work. I am not sure if it is the right code. How should I fix it?
function demoTaylorlog(x0,dx)
% demoTaylor Taylor Series approximations for f(x) = 1/(1-x)
%Synopsis:
% demoTaylorlog(x0,dx)
% Input: x0 = (optional) point about which the Taylor Series expansion is
% made. Default: x0 = 1.6;
% dx = (optional) size of neighborhood over which the expansion
% is evaluated. Default: dx = 0.8
% Output: a plot of f(x) and its Taylor Series approximations
if nargin<2, x0 = 1.6; dx = 0.8; end
x = linspace(x0-dx/2,x0+dx/2,20);
% x-values at which f(x) is evaluated
f(x)= log(x);
% Exact f(x); notice the array operator
h = x - x0;
% Avoid recomputing intermediate values,
t = 1/(1-x0);
% h and t p1x = t*ones(size(x)) + h*t^2;
% First order Taylor polynomial p2x = p1x+ (h.^2)*t^3;
% Second order " " " p3x = p2x + (h.^3)*t^4;
% Third
plot(x,fx,'-',x,p1x,'o-',x,p2x,'^-',x,p3x,'s-');
legend('exact','P_1(x)','P_2(x)','P_3(x)',4);
xlabel('x');
ylabel('Approximations to f(x) = 1/(1-x)');
end
In the statement f(x)= log(x); f is a vector and log is a function. Suppose that your x vector is [0.12 0.24 0.36] then the statement with the error is equivalent to:
f(0.12) = log(0.12);
f(0.24) = log(0.24);
f(0.36) = log(0.36);
But if fis a vector the assignment to f(0.12) has no sense because .12 is not a positive integer or a logical value (as the error says).
You should write f = log(x);

MATLAB Create surface plot of function for all X, Y pairs

I have a custom function called dissmeasure that outputs a scalar from an input vector of frequencies. Another function called music.tone2freq converts integers numbers to frequencies.
My objective is to create a surface plot of dissmeasure for all pairs of x,y integers where X and Y equal [0:1:11].
It should look something like this (this is mesh(X,Y, ones(12,12) ) ):
Following from the mesh docs, what I have tried is doing:
[X,Y] = meshgrid(0:1:12)
Z = dissmeasure(music.tone2freq([X., Y.]))
But I get Error: Expression or statement is incorrect--possibly unbalanced (, {, or [..
Z = dissmeasure(music.tone2freq([X(:), Y(:)]))
But [X(:), Y(:)] doesn't seem to have the correct size. Also my function that accepts a vector returns one scalar for that whole input. What I need is multiple returns.
Note that both dissmeasure(music.tone2freq([X(:), Y(:)])) and dissmeasure(music.tone2freq([X, Y])) work, but the result from dissmeasure is a single scalar number, not a matrix of the result of this function for each x,y pair.
Any help? Thank you
tone2freq.m
function f = tone2freq(T)
% MUSIC.TONE2FREQ converts a musical semitone to a frequency.
% F = MUSIC.TONE2FREQ(T) converts the musical semitones in T to frequencies.
%
% Example
% f = music.tone2freq(0:2); % returns [261.63 277.19 293.67]
%
% See also music.tone2interval, music.tone2note, music.freq2tone.
% Author: E. Johnson
% Copyright 2010 The MathWorks, Inc.
fC4 = 261.625565300599; % Middle C (C4) is 261.63 Hz
f = fC4 .* 2 .^ (T / 12);
dissmeasure.m:
% calculate dissonace
% input param fvec - list of frequencies
% input param amp - list of amplitudes
% output is sum of dissonances of each pair of partials (scalar)
function d = dissmeasure(fvec, amp)
if ~exist('amp','var')
amp = ones(size(fvec));
end
Xstar = 0.24; % place with maximum dissonance
S1 = 0.0207; % to fit frequency dependend curves
S2 = 18.96; % so max. dissonance occures at 1/4 critical bandwidth
C1 = 5;
C2 = -5;
B1 = -3.51; % derived from model of Levelt & Plomp
B2 = -5.75;
N = length(fvec);
[fvec, idx_list] = sort(fvec); % sort partial frequencies ascending
amp = amp(idx_list); % rearrange amplitude values
%amp = loudness(amp);
D = 0;
for i=2:N
Fmin = fvec(1 : N-i+1); % get slice as list of Fmin
S = Xstar./(S1*Fmin+S2); % calc list of s-scalings with list of Fmin
% treat vector as tail and head ...
Fdif = fvec(i:N) - fvec(1:N-i+1); % build element wise difference
a = min(amp(i:N), amp(1:N-i+1)); % select element wise a minimum
Dnew = a .* (C1*exp(B1*S.*Fdif) + C2*exp(B2*S.*Fdif));
D = D + sum(Dnew); % sum up last D and vector elements
end
d=D;
Your function dissmeasure does not support vectorized operations, which means that for inputs of size N the function is evaluated for each element and an output of size N is returned.
Instead your function returns the summarized dissonance.
%assuming you have X and Y already converted
Z=X*0 % initialize Z of same size
for ix = 1:numel(X)
Z(ix)=dissmeasure(X(ix),Y(ix));
end

Multiplying a vector times the inverse of a matrix in Matlab

I have a problem multiplying a vector times the inverse of a matrix in Matlab. The code I am using is the following:
% Final Time
T = 0.1;
% Number of grid cells
N=20;
%N=40;
L=20;
% Delta x
dx=1/N
% define cell centers
%x = 0+dx*0.5:dx:1-0.5*dx;
x = linspace(-L/2, L/2, N)';
%define number of time steps
NTime = 100; %NB! Stability conditions-dersom NTime var 50 ville en fått helt feil svar pga lambda>0,5
%NTime = 30;
%NTime = 10;
%NTime = 20;
%NTime = 4*21;
%NTime = 4*19;
% Time step dt
dt = T/NTime
% Define a vector that is useful for handling teh different cells
J = 1:N; % number the cells of the domain
J1 = 2:N-1; % the interior cells
J2 = 1:N-1; % numbering of the cell interfaces
%define vector for initial data
u0 = zeros(1,N);
L = x<0.5;
u0(L) = 0;
u0(~L) = 1;
plot(x,u0,'-r')
grid on
hold on
% define vector for solution
u = zeros(1,N);
u_old = zeros(1,N);
% useful quantity for the discrete scheme
r = dt/dx^2
mu = dt/dx;
% calculate the numerical solution u by going through a loop of NTime number
% of time steps
A=zeros(N,N);
alpha(1)=A(1,1);
d(1)=alpha(1);
b(1)=0;
c(1)=b(1);
gamma(1,2)=A(1,2);
% initial state
u_old = u0;
pause
for j = 2:NTime
A(j,j)=1+2*r;
A(j,j-1)=-(1/dx^2);
A(j,j+1)=-(1/dx^2);
u=u_old./A;
% plotting
plot(x,u,'-')
xlabel('X')
ylabel('P(X)')
hold on
grid on
% update "u_old" before you move forward to the next time level
u_old = u;
pause
end
hold off
The error message I get is:
Matrix dimensions must agree.
Error in Implicit_new (line 72)
u=u_old./A;
My question is therefore how it is possible to perform u=u_old*[A^(-1)] in Matlab?
David
As knedlsepp said, v./A is the elementwise division, which is not what you wanted. You can use either
v/A provided that v is a row vector and its length is equal to the number of columns in A. The result is a row vector.
A\v provided that v is a column vector and its length is equal to the number of rows in A
The results differ only in shape: v/A is the transpose of A'\v'

Using MATLAB to write a function that implements Newton's method in two dimensions

I am trying to write a function that implements Newton's method in two dimensions and whilst I have done this, I have to now adjust my script so that the input parameters of my function must be f(x) in a column vector, the Jacobian matrix of f(x), the initial guess x0 and the tolerance where the function f(x) and its Jacobian matrix are in separate .m files.
As an example of a script I wrote that implements Newton's method, I have:
n=0; %initialize iteration counter
eps=1; %initialize error
x=[1;1]; %set starting value
%Computation loop
while eps>1e-10&n<100
g=[x(1)^2+x(2)^3-1;x(1)^4-x(2)^4+x(1)*x(2)]; %g(x)
eps=abs(g(1))+abs(g(2)); %error
Jg=[2*x(1),3*x(2)^2;4*x(1)^3+x(2),-4*x(2)^3+x(1)]; %Jacobian
y=x-Jg\g; %iterate
x=y; %update x
n=n+1; %counter+1
end
n,x,eps %display end values
So with this script, I had implemented the function and the Jacobian matrix into the actual script and I am struggling to work out how I can actually create a script with the input parameters required.
Thanks!
If you don't mind, I'd like to restructure your code so that it is more dynamic and more user friendly to read.
Let's start with some preliminaries. If you want to make your script truly dynamic, then I would recommend that you use the Symbolic Math Toolbox. This way, you can use MATLAB to tackle derivatives of functions for you. You first need to use the syms command, followed by any variable you want. This tells MATLAB that you are now going to treat this variable as "symbolic" (i.e. not a constant). Let's start with some basics:
syms x;
y = 2*x^2 + 6*x + 3;
dy = diff(y); % Derivative with respect to x. Should give 4*x + 6;
out = subs(y, 3); % The subs command will substitute all x's in y with the value 3
% This should give 2*(3^2) + 6*3 + 3 = 39
Because this is 2D, we're going to need 2D functions... so let's define x and y as variables. The way you call the subs command will be slightly different:
syms x, y; % Two variables now
z = 2*x*y^2 + 6*y + x;
dzx = diff(z, 'x'); % Differentiate with respect to x - Should give 2*y^2 + 1
dzy = diff(z, 'y'); % Differentiate with respect to y - Should give 4*x*y + 6
out = subs(z, {x, y}, [2, 3]); % For z, with variables x,y, substitute x = 2, y = 3
% Should give 56
One more thing... we can place equations into vectors or matrices and use subs to simultaneously substitute all values of x and y into each equation.
syms x, y;
z1 = 3*x + 6*y + 3;
z2 = 3*y + 4*y + 4;
f = [z1; z2];
out = subs(f, {x,y}, [2, 3]); % Produces a 2 x 1 vector with [27; 25]
We can do the same thing for matrices, but for brevity I won't show you how to do that. I will defer to the code and you can see it then.
Now that we have that established, let's tackle your code one piece at a time to truly make this dynamic. Your function requires the initial guess x0, the function f(x) as a column vector, the Jacobian matrix as a 2 x 2 matrix and the tolerance tol.
Before you run your script, you will need to generate your parameters:
syms x y; % Make x,y symbolic
f1 = x^2 + y^3 - 1; % Make your two equations (from your example)
f2 = x^4 - y^4 + x*y;
f = [f1; f2]; % f(x) vector
% Jacobian matrix
J = [diff(f1, 'x') diff(f1, 'y'); diff(f2, 'x') diff(f2, 'y')];
% Initial vector
x0 = [1; 1];
% Tolerance:
tol = 1e-10;
Now, make your script into a function:
% To run in MATLAB, do:
% [n, xout, tol] = Jacobian2D(f, J, x0, tol);
% disp('n = '); disp(n); disp('x = '); disp(xout); disp('tol = '); disp(tol);
function [n, xout, tol] = Jacobian2D(f, J, x0, tol)
% Just to be sure...
syms x, y;
% Initialize error
ep = 1; % Note: eps is a reserved keyword in MATLAB
% Initialize counter
n = 0;
% For the beginning of the loop
% Must transpose into a row vector as this is required by subs
xout = x0';
% Computation loop
while ep > tol && n < 100
g = subs(f, {x,y}, xout); %g(x)
ep = abs(g(1)) + abs(g(2)); %error
Jg = subs(J, {x,y}, xout); %Jacobian
yout = xout - Jg\g; %iterate
xout = yout; %update x
n = n + 1; %counter+1
end
% Transpose and convert back to number representation
xout = double(xout');
I should probably tell you that when you're doing computation using the Symbolic Math Toolbox, the data type of the numbers as you're calculating them are a sym object. You probably want to convert these back into real numbers and so you can use double to cast them back. However, if you leave them in the sym format, it displays your numbers as neat fractions if that's what you're looking for. Cast to double if you want the decimal point representation.
Now when you run this function, it should give you what you're looking for. I have not tested this code, but I'm pretty sure this will work.
Happy to answer any more questions you may have. Hope this helps.
Cheers!