Use Matlab Function in Mathematics

Matlab Function, Matlab command “diff”, Vector Inputs, Derivative

In this sample solution demonstrated by the matlab tutor, there is an application of Matlab function and Matlab command concept. The expert has revealed ‘how to write Matlab functions Intcompare, Splinecalc, and Hermitecalc under the given constraints. The functions take stated inputs and furnish predetermined output. In one of the parts of the assignment, the expert has demonstrated the use of Matlab command “diff” for calculation of derivative of a function.

SOLUTION : –

function ret = intcompare(x, f, a, b)

syms xs

y = double(f(x));

n = length(x);

[ai,bi,ci,di] = splinecalc(x,f);

[Q,z] = hermitecalc(x,f);

%% Let’s build the function S(x)

for i = 1:n

S(i) = ai(i) + bi(i)*(xs-x(i)) + ci(i)*(xs-x(i))^2 + di(i)*(xs-x(i))^2;

end

real_integral_val = double(int(f, xs, a, b));

piece_int = 0;

for i = 1:n-1

piece_int = piece_int + double(int(S(i), xs, x(i), x(i+1)));

end

%% Hermite poly

k = 2*(n-1)+1;

Sh(xs) = Q(k+1,k+1)*(xs-z(k));

for i = 2:k

j = k-i+1;

Sh(xs) = (Sh(xs) + Q(j+1,j+1))*(xs-z(j));

end

Sh(xs) = Sh(xs) + Q(1,1);

hermite_integral = double(int(Sh, xs, a,b));

ret = zeros(3,3);

ret(1,1) = real_integral_val;

ret(1,2) = piece_int;

ret(1,3) = hermite_integral;

ret(2,1) = 0;

ret(2,2) = piece_int-real_integral_val;

ret(2,3) = hermite_integral-real_integral_val;

ret(3,1) = 0;

ret(3,2) = ret(2,2)/real_integral_val;

ret(3,3) = ret(2,3)/real_integral_val;

end

function [a,b,c,d] = splinecalc(x, f)

% Campled Cubic Splien Algorithm (3.5)

syms xs

n = length(x)-1;

y = double(subs(f,x));

a = y;

FPO = double(subs(diff(f,xs), x(1)));

FPN = double(subs(diff(f,xs), x(n)));

% Step 1

M = n-1;

h = zeros(1,M+1);

for i = 0:M

h(i+1) = x(i+2)-x(i+1);

end

%Step 2

alfa = zeros(1,n+1);

alfa(1) = 3*(a(2)-a(1))/h(1) – 3*FPO;

alfa(n+1) = 3*FPN – 3*(a(n+1) – a(n))/h(n);

% Step 3

for i = 1:M

alfa(i+1) = 3*(a(i+2)*h(i)-a(i+1)*(x(i+2)-x(i)) + a(i)*h(i+1))/(h(i+1)*h(i));

end

%Step 4

I = zeros(1,n+1);

u = zeros(1,n+1);

z = zeros(1,n+1);

I(1) = 2*h(1);

u(1) = 0.5;

z(1) = alfa(1)/I(1);

% Step 5

for i = 1:M

I(i+1) = 2*(x(i+2)-x(i)) – h(i)*u(i);

u(i+1) = h(i+1)/I(i+1);

z(i+1) = (alfa(i+1) – h(i)*z(i))/I(i+1);

end

% Step 6

I(n+1) = h(n)*(2-u(n));

z(n+1) = (alfa(n+1) – h(n)*z(n))/I(n+1);

c = zeros(1,n+1);

b = zeros(1,n+1);

d = zeros(1,n+1);

c(n+1) = z(n+1);

% Step 7

for i = 1:n

j = n-i;

c(j+1) = z(j+1) – u(j+1)*c(j+2);

b(j+1) = (a(j+2) – a(j+1))/h(j+1) – h(j+1)*(c(j+2)+2*c(j+1))/3;

d(j+1) = (c(j+2) – c(j+1))/(3*h(j+1));

end

end

function [Q,z] = hermitecalc(x, f)

syms xs

fp = diff(f,xs);

% Algorithm 3.3

n = length(x)-1;

Q = zeros(2*n+2, 2*n+2);

% Step 1

z = zeros(1,2*n+2);

for i = 0:n

% Step 2

z(2*i+1) = x(i+1);

z(2*i+2) = x(i+1);

Q(2*i+1,1) = double(subs(f(x(i+1))));

Q(2*i+2,1) = double(subs(f(x(i+1))));

Q(2*i+2,2) = double(subs(fp(x(i+1))));

% Step 3

if i ~= 0

Q(2*i+1,2) = (Q(2*i+1,1) – Q(2*i, 1))/(z(2*i+1) – z(2*i));

end

end

% Step 4

for i = 2:2*n+1

for j = 2:i

Q(i+1,j+1) = (Q(i+1,j) – Q(i, j))/(z(i+1) – z(i-j+1));

end

end

end

clc, clear all, close all

syms xs                 % Variable used to define a symbolic function

f(xs) = exp(xs)^2;      % Symbolic function to be evaluated

x = 0:3;                % x-data [0 1 2 3]

y = double(f(x));       % y data f(x)

% Coeficients a and b

ap= x(1);

bp = x(end);

n = length(x);

%% Calculate and display spline coefficients

[a,b,c,d] = splinecalc(x,f);

spline_coefficients = [a;b;c;d];

%% Calculate and display intcompare solution

intcompare_solution = intcompare(x,f,x(1),x(end))

%% Calculate and display hermite polynomials

[Q,z] = hermitecalc(x,f);

hermite_coefficients = Q;

%% Plot interpolations

figure(1)

xspan = linspace(ap,bp,20);

plot(xspan, f(xspan), ‘–mo’, ‘color’, [0.8500, 0.3250, 0.0980], ‘linewidth’, 3) %red

hold on

% spline_plot = zeros(1,(n-1)*20);

for i = 1:n-1

lb = x(i);

if i < n

ub = x(i+1);

else

ub = 2*x(i);

end

xspan = linspace(lb,ub,20);

spline_plot((i-1)*20+1:i*20) = a(i) + b(i).*(xspan-x(i)) + c(i)*(xspan-x(i)).^2 + d(i).*(xspan-x(i)).^3;

plot(xspan, a(i) + b(i).*(xspan-x(i)) + c(i)*(xspan-x(i)).^2 + d(i).*(xspan-x(i)).^3, ‘:’, ‘color’, [0, 0.4470, 0.7410], ‘linewidth’, 2);

hold on

end

%% Hermite poly

k = 2*(n-1)+1;

Sh(xs) = Q(k+1,k+1)*(xs-z(k));

for i = 2:k

j = k-i+1;

Sh(xs) = (Sh(xs) + Q(j+1,j+1))*(xs-z(j));

end

Sh(xs) = Sh(xs) + Q(1,1);

xspan = linspace(ap,bp,20);

plot(xspan, Sh(xspan), ‘–‘, ‘color’, [0.9290, 0.6940, 0.1250], ‘linewidth’, 2)

legend(‘Real Values’, ‘Spline’, ‘Hermite’);

% [h,~] = legend(‘show’)

grid minor

title(‘Comparisson of interpolations’);

xlabel(‘x’);

ylabel(‘f(x)’);

clc, clear all, close all

syms xs                 % Variable used to define a symbolic function

f(xs) = exp(xs)^2;      % Symbolic function to be evaluated

x = 0:3;                % x-data [0 1 2 3]

y = double(f(x));       % y data f(x)

% Coeficients a and b

ap= x(1);

bp = x(end);

n = length(x);

%% Calculate and display spline coefficients

[a,b,c,d] = splinecalc(x,f);

spline_coefficients = [a;b;c;d];

%% Calculate and display intcompare solution

intcompare_solution = intcompare(x,f,x(1),x(end))

%% Calculate and display hermite polynomials

[Q,z] = hermitecalc(x,f);

hermite_coefficients = Q;

%% Plot interpolations

figure(1)

xspan = linspace(ap,bp,20);

plot(xspan, f(xspan), ‘–mo’, ‘color’, [0.8500, 0.3250, 0.0980], ‘linewidth’, 3) %red

hold on

% spline_plot = zeros(1,(n-1)*20);

for i = 1:n-1

lb = x(i);

if i < n

ub = x(i+1);

else

ub = 2*x(i);

end

xspan = linspace(lb,ub,20);

spline_plot((i-1)*20+1:i*20) = a(i) + b(i).*(xspan-x(i)) + c(i)*(xspan-x(i)).^2 + d(i).*(xspan-x(i)).^3;

plot(xspan, a(i) + b(i).*(xspan-x(i)) + c(i)*(xspan-x(i)).^2 + d(i).*(xspan-x(i)).^3, ‘:’, ‘color’, [0, 0.4470, 0.7410], ‘linewidth’, 2);

hold on

end

%% Hermite poly

k = 2*(n-1)+1;

Sh(xs) = Q(k+1,k+1)*(xs-z(k));

for i = 2:k

j = k-i+1;

Sh(xs) = (Sh(xs) + Q(j+1,j+1))*(xs-z(j));

end

Sh(xs) = Sh(xs) + Q(1,1);

xspan = linspace(ap,bp,20);

plot(xspan, Sh(xspan), ‘–‘, ‘color’, [0.9290, 0.6940, 0.1250], ‘linewidth’, 2)

legend(‘Real Values’, ‘Spline’, ‘Hermite’);

% [h,~] = legend(‘show’)

grid minor

title(‘Comparisson of interpolations’);

xlabel(‘x’);

ylabel(‘f(x)’);