Attempt to plot an piecewise function
I am trying to implemment an algorithm for Rayleigh-Ritz method using B-Spline basis ( see Burden, numerical analysis, 9th ed. section 11.5). The fuction S is the following:
my attempt is:
S = @(x) ( (x <= -2) ) .* 0 + …
( (x > -2) & (x <= -1) ) .* (1/4 * (2 + x).^3) + …
( (x > -1) & (x <= 0) ) .* (1/4 * (2 + x).^3 – 4 * (1 + x).^3) + …
( (x > 0) & (x <= 1) ) .* (1/4 * (2 – x).^3 – 4 * (1 – x).^3) + …
( (x > 1) & (x <= 2) ) .* (1/4 * (2 – x).^3) + …
( (x > 2) ) .* 0;
% Plotting the function
y_v = linspace(-3, 3, 100); % Extend the range a bit to see behavior around boundaries
s_valores = arrayfun(S, y_v);
plot(y_v, s_valores);
xlabel(‘x’);
ylabel(‘S(x)’);
title(‘Piecewise Function S(x)’);
grid on;
I was expecting the graph to be like
I run it in Octave, but it is very similar to Matlab. Can you point what I am missing?I am trying to implemment an algorithm for Rayleigh-Ritz method using B-Spline basis ( see Burden, numerical analysis, 9th ed. section 11.5). The fuction S is the following:
my attempt is:
S = @(x) ( (x <= -2) ) .* 0 + …
( (x > -2) & (x <= -1) ) .* (1/4 * (2 + x).^3) + …
( (x > -1) & (x <= 0) ) .* (1/4 * (2 + x).^3 – 4 * (1 + x).^3) + …
( (x > 0) & (x <= 1) ) .* (1/4 * (2 – x).^3 – 4 * (1 – x).^3) + …
( (x > 1) & (x <= 2) ) .* (1/4 * (2 – x).^3) + …
( (x > 2) ) .* 0;
% Plotting the function
y_v = linspace(-3, 3, 100); % Extend the range a bit to see behavior around boundaries
s_valores = arrayfun(S, y_v);
plot(y_v, s_valores);
xlabel(‘x’);
ylabel(‘S(x)’);
title(‘Piecewise Function S(x)’);
grid on;
I was expecting the graph to be like
I run it in Octave, but it is very similar to Matlab. Can you point what I am missing? I am trying to implemment an algorithm for Rayleigh-Ritz method using B-Spline basis ( see Burden, numerical analysis, 9th ed. section 11.5). The fuction S is the following:
my attempt is:
S = @(x) ( (x <= -2) ) .* 0 + …
( (x > -2) & (x <= -1) ) .* (1/4 * (2 + x).^3) + …
( (x > -1) & (x <= 0) ) .* (1/4 * (2 + x).^3 – 4 * (1 + x).^3) + …
( (x > 0) & (x <= 1) ) .* (1/4 * (2 – x).^3 – 4 * (1 – x).^3) + …
( (x > 1) & (x <= 2) ) .* (1/4 * (2 – x).^3) + …
( (x > 2) ) .* 0;
% Plotting the function
y_v = linspace(-3, 3, 100); % Extend the range a bit to see behavior around boundaries
s_valores = arrayfun(S, y_v);
plot(y_v, s_valores);
xlabel(‘x’);
ylabel(‘S(x)’);
title(‘Piecewise Function S(x)’);
grid on;
I was expecting the graph to be like
I run it in Octave, but it is very similar to Matlab. Can you point what I am missing? plot, plotting MATLAB Answers — New Questions