Nodal basis function 1D

M
mohamadoseini · Mar 23, 2022 · 1.9K views
Question
Hello all, I coded a nodal basis function for 1D element from [-1,1]. the code is below:   close all; clc; clearvars; n=10; x = linspace(-1,1,n); for i=1:n a= x(i); for j=1:n b(j)=a.^(j-1); end v(i,:)=b'; end vinv=inv(v); for i=1:n k=zeros(1,n); k(i)=1; f=vinv*k' p(:,i)=f; end for i=1:n g=@(x) p(1,i)+p(2,i).*x+p(3,i).*x.^2+p(4,i).*x.^3+p(5,i).*x.^4+p(6,i).*x.^5+p(7,i).*x.^6+p(8,i).*x.^7+p(9,i).*x.^8+ .... p(10,i).*x.^9; legendInfo{i} = ['Phi ' num2str(i)]; fplot(g, [-1 1]) legend(legendInfo) hold on; end The code works already but my problem is in last "for loop" where I calculated "g" as a function handle. I want to instead of adding the terms from 1 to 10, use an automated calculation. Now, if I want to change number of nodes (n) from 10 to 20 I have to add 10 additional terms by hand. Moreover, Does somebody knows a better way to calculate nodal basis function for 1D element? Great thanks,
Expert Answer
Profile picture of John Williams
John Williams PhD Expert
Answered Sep 13, 2026






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.





100% Run Guarantee 3-Hour Fast-Track Delivery

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.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!