Evaluating a function over a given interval for plotting - matlab

I'm supposed to plot a certain Lagrange polynomial over a given interval [a,b] using a function that evaluates this polynomial pointwise:
function y = evalLagrangePolynomial(X, k, x)
n = numel(X);
y = 1;
for i=1:n
if i ~= k
y = y * (x - X(i)) / (X(k) - X(i));
end
end
end
However i'm a little confused how to tackle this problem; is there some native matlab function for this kind of situation?

I don't know of a native function but, here's one I wrote up.
XY = sortrows(rand(10, 2));
X = XY(:,1);
Y = XY(:,2);
x = linspace(0, 1, 100);
y = LagrangePolynomial(X, Y, x);
plot(x, y, '-', X, Y, 'o')
With the function
function y = LagrangePolynomial(X, Y, x)
if isscalar(x)
l = GenerateLagrangePolynomial(X, x)';
y = sum(l .* Y);
else
y = arrayfun(#(w) LagrangePolynomial(X, Y, w), x);
end
end
function l = GenerateLagrangePolynomial(X, x)
[X, ~] = meshgrid(X, ones(size(X)));
lPreprod = (x - X') ./ (X - X');
lPreprod(isinf(lPreprod)) = 1;
l = prod(lPreprod, 1);
end

Related

Issue with Lagrange interpolation in Matlab

I'm having an issue completing this Lagrange function and I'm not sure where I'm going wrong.
I keep getting 'Index exceeds matrix dimensions' error. I assume this is because I'm not moving through the array correctly. I've tried a combination of things but seem to continue coming back to this error.
function y = lagrange(X, Y, x)
n = length(X);
if n ~= length(Y)
error('X and Y must have the same length.');
end
y = zeros(size(x));
for i = 1:n
L = ones(size(x));
for j = [1:i-1 i+1:n]
if (i~=j)
L(1:i-1, i+1:n) = L(1:i-1, i+1:n).*(x-X(j))/(X(i)-X(j));
end
end
y = y+Y(i)*L(1:i-1);
end
I think the function below is what you need.
function y = lagrangian(X, Y, x)
if length(X) ~= length(Y); error('X and Y must have the same length.'); end
y = zeros(size(x));
for i = 1:length(X)
L = ones(size(x));
for j = [1:i-1 i+1:length(X)]
L = L.*(x-X(j))/(X(i)-X(j));
end
y = y+Y(i)*L;
end

Equation to represent mountain-with-hollow-like surface

I want to use function where x and y are coordinates forming circle, z(x, y) grows from 0 at the edge to max at the R/2 and back to 0 at the center without sharp changes. I stucked with
t = -pi:pi/180:pi;
R = 5;
x = R*cos(t);
y = R*sin(t);
for i = 1:361
for j = 1:361
z(i,j) = exp( sqrt((x(i)).^2 + (y(j)).^2));
end
end
[u, v] = meshgrid(x, y);
mesh(u, v, z), grid on;
How should I realize z to add this drop to the middle? Any suggestions appreciated!
Maybe subtract two 2D gaussians with a different standard deviation?
% the area
x = linspace(-5,5,1E2);
y = linspace(-5,5,1E2);
sig1=1;
sig2=2;
%2D gaussian
efac = 1/(2*sig1);
X = exp(-efac*x.^2);
Y = exp(-efac*y.^2)';
z1 = Y*X;
z1=z1./max(z1(:));
%2D gaussian
efac = 1/(2*sig2);
X = exp(-efac*x.^2);
Y = exp(-efac*y.^2)';
z2 = Y*X;
z2=z2./max(z2(:));
[u, v] = meshgrid(x, y);
mesh(u, v, z2-z1), grid on;
An alternative that has zero outside of R and is more along the lines of your own code:
x = linspace(-2*pi,2*pi,1E2);
y = linspace(-2*pi,2*pi,1E2);
[u, v] = meshgrid(x, y);
r = sqrt(u.^2+v.^2);
z = sin(r);
z(r>pi)=0;
mesh(u, v, z), grid on;
Or to make it a little less sharp at the base:
z = sin(r).^2;

Finding x intercept in MATLAB

I am trying to find the x intercept of a parabola of a plot using
x0 = interp1(y,x,0)
However because my parabola starts at the origin it returns 0.
How do I find the x intercept that lies away from the origin? At the moment I am estimating by eye-ball.
Code for the plot:
global k g
g = 10;
v0 = 150;
theta = pi/4;
m = 1;
k = 0.001;
tspan = [0 22];
IC = [0; v0*cos(theta); 0; v0*sin(theta)];
[t, oput] = ode45(#dataODE, tspan, IC);
x = oput(:,1);
vx = oput(:,2);
y = oput(:,3);
vy = oput(:,4);
figure(1); clf;
plot(x,y)
where
function [p] = dataODE( t, x)
global k g
p = zeros(4,1);
p(1) = x(2);
p(2) = -k*sqrt(x(2)^2 + x(4)^2)* x(2);
p(3) = x(4);
p(4) = -g -k*sqrt(x(2)^2 + x(4)^2)* x(4);
You could just restrict x and y to only contain the one intercept:
x0 = interp1(y(a:b),x(a:b),0)
but how do you find a and b? One way would be to just use the points before and after y crosses zero (on the way down):
a = find(diff(y > 0) == -1)
b = a+1

Contour plot for 3D vector

I am trying to produce a contour plot for the 3D vectors returned by a custom function in the xy plane where z = 0.
I tried this but I just get an empty graph:
% Stand in for the real function I want to plot.
f = #(x, y, z) [x ^ 2, y ^ 2, x * y + z];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y), 3);
% I know this can be vectorized but the function I really want to graph can't
% be.
for i = 1:length(x)
for j = 1:length(y)
z(i, j, :) = f(x(i), y(j), 0);
end
end
figure;
axis equal;
contour(x, y, z);
You should mention what your axis will be. You have x,y and 3 outputs from f.
If you consider 3 outputs of your f as the ones to be plotted then you should use,
contour(z(:,:,1),z(:,:,2),z(:,:,3));
Which will give you this,
I think what you are looking for, is a function with one output, like,
f = #(x, y,z) [x ^ 2 + y ^ 2 + x * y + z ];
x = linspace(-5, 5, 50);
y = linspace(-5, 5, 50);
z = zeros(length(x), length(y));
for i = 1:length(x)
for j = 1:length(y)
z(i, j) = f(x(i), y(j),0);
end
end
contour(x,y,z,20);

Working with meshgrid in MATLAB

How would you replace the following by meshgrid?
N=.33;
P=.45;
for i =1:10
for j=1:20
x0=i+N;
y0=j+P;
f(i,j)=exp(x0+y0);
end
end
I think something like
[X, Y] = meshgrid(1:10, 1:20);
Z = exp( (X+N) + (Y+P) );
% surf(X, Y, Z);
read here
edit
as user requested (if I've understood well):
[X, Y] = meshgrid(1:10, 1:20);
X1 = X + N; Y1 = Y + P;
Z = exp( X1 + Y1 );
% surf(X1, Y1, Z);