1D Nodal Basis Function Implementation in MATLAB
A nodal basis function (hat function) has a value of 1 at its own node and drops to 0 at all other nodes. This property allows piecewise linear interpolation across finite element meshes.
1. MATLAB Script: Generate and Plot Global Hat Functions
This script sets up a 1D grid of node points, evaluates the basis functions, and plots the complete family of hat functions.
% Define node coordinates
nodes = [0.0, 0.25, 0.5, 0.75, 1.0];
numNodes = length(nodes);
% Fine grid for plotting smooth lines
xFine = linspace(nodes(1), nodes(end), 600);
figure('Color', 'w', 'Position', [100, 100, 750, 400]);
hold on;
colors = lines(numNodes);
% Compute and plot basis function for each node
for i = 1:numNodes
basisValues = zeros(size(xFine));
for k = 1:length(xFine)
x = xFine(k);
% Left slope of the hat
if i > 1 && x >= nodes(i-1) && x <= nodes(i)
basisValues(k) = (x - nodes(i-1)) / (nodes(i) - nodes(i-1));
% Right slope of the hat
elseif i < numNodes && x >= nodes(i) && x <= nodes(i+1)
basisValues(k) = (nodes(i+1) - x) / (nodes(i+1) - nodes(i));
end
end
plot(xFine, basisValues, 'LineWidth', 1.8, 'Color', colors(i,:), ...
'DisplayName', sprintf('Node %d', i));
end
% Highlight nodes on plot
plot(nodes, ones(size(nodes)), 'ko', 'MarkerFaceColor', 'k', 'DisplayName', 'Peak (Value = 1)');
plot(nodes, zeros(size(nodes)), 'rs', 'MarkerFaceColor', 'r', 'DisplayName', 'Zeros (Value = 0)');
xlabel('Position X');
ylabel('Basis Value');
title('1D Piecewise Linear Nodal Basis Functions');
legend('Location', 'northeastoutside');
grid on;
ylim([-0.1, 1.1]);
2. MATLAB Function: Reusable Evaluator
Use this function to interpolate any nodal field values at arbitrary query points.
function uInterp = interpolate1D(nodes, nodalValues, xQuery)
% Interpolates field values using 1D nodal basis functions
% nodes: Vector of node positions [x1, x2, ..., xN]
% nodalValues: Field values at each node [u1, u2, ..., uN]
% xQuery: Points where you want the interpolated result
numNodes = length(nodes);
uInterp = zeros(size(xQuery));
for i = 1:numNodes
phi_i = zeros(size(xQuery));
for k = 1:length(xQuery)
x = xQuery(k);
if i > 1 && x >= nodes(i-1) && x <= nodes(i)
phi_i(k) = (x - nodes(i-1)) / (nodes(i) - nodes(i-1));
elseif i < numNodes && x >= nodes(i) && x <= nodes(i+1)
phi_i(k) = (nodes(i+1) - x) / (nodes(i+1) - nodes(i));
end
end
uInterp = uInterp + nodalValues(i) * phi_i;
end
end
3. Test Interpolation Example
% Node positions and recorded sensor values at each node
gridNodes = [0, 2, 4, 6, 8, 10];
sensorReadings = [10, 25, 40, 30, 15, 5];
% Query value at x = 3.5 (between node 2 and node 3)
xTest = 3.5;
estimatedValue = interpolate1D(gridNodes, sensorReadings, xTest);
fprintf('Interpolated value at x = %.1f is %.2f\n', xTest, estimatedValue);
% Output: Interpolated value at x = 3.5 is 36.25
Key Implementation Rules:
- Each basis function is non-zero only within elements that touch its assigned node.
- The sum of all basis function values at any single location in the grid always equals 1.
- To interpolate a value at point
x, multiply each node value by its basis function value and sum them up.
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Explore similar technical troubleshooting questions and verified MATLAB solutions: