__Matlab Function, Matlab command “diff”, Vector Inputs, Derivative__

__Matlab Function, Matlab command “diff”, Vector Inputs, Derivative__

**Use Matlab functions for mathematics.**

A beginner in programming might be hearing of the word functions in programming for the first time. It’s much easier for you to get confused with the term mathematical functions, especially if you have a mathematical background. The functions described here are computer programming functions. These functions are the main building blocks for each code that you would write. If you want to accomplish a complicated task, you must incorporate its use. Nearly every task that you perform in Matlab involvesa use offunctions.

For a mathematics assignment, Matlab has a large number of functions that you could utilize. You might be wonderingwhether itis possible to use Matlab functions for mathematics? Do not worry. This article would briefyou aboutthe functions which you can apply in your mathematics assignment.

**What are functions?**

Whichever programming language youmay use, you will come across this term. It isn’t as complicated as you may think. it’s a simple one. Perhaps you might have used it many a times before. In the computer programming realm, we define a function as a reusable set of lines of code. Functions can return a value or perform a precise operation. In some computer languages, the word a function and a procedure can be used alternatively.

Types of functions.

Functions can be classified in various ways. The common ways of categorizing functions are:-

#### 1. Inbuilt and user-defined functions.

In Matlab, you might have come across the function fprint. Well, this is an example of an inbuilt function. Sometimes they are referred to as readymade functions. We define an inbuilt function as a function that comes with the libraries. When you decide to use a toolbox such as the signal processing toolbox, it will come with the function that you can use. These inbuilt functions have a syntax that you must adhere to. Failure could lead to errors. Therefore, to avoid such issues, you must read the related documents.

User-defined functions are the ones that you have to create yourself. These functions require you to declare the variables that you would use and the conditions which the variables satisfy. Matlab has a unique way of creating a function. To create a function, you use Matlab’s inbuilt function known as function at the beginning, and you close the function by using the end statement. Let’s show you an example. Say you want to create a function pack.

function f = pack(x, y)

‘Your statements’

end

From the above function, you can note that we made a use of values x and y. These are the variables the function needs and whenever you call this function you must state these variables. Variable f is a function that is used inside the function statement and has an impact.

#### 2. Returns a value or performs an operation.

Here is another way of classifying a function based on the results. By functions that return a value, we imply those functions that yield a numerical value, string, matrix or a logical value. Functions that return a value are the ones that we will use a lot in most mathematical courses. For instance, you might be required to conduct a hypothesis test of a population. In this case, you need to compute the p-value.

Functions that perform an operation are very common. They do not yield anything. They include the function that deletes the item in the console. Their use is less common in mathematical manipulations.

#### 3. Local or nested functions

Here we classify user made functions depending on the structure in a file. Note it’s generally possible for a code file to have more than one function. A local function is the most used by many, as it’s much easier. They are the normal functions that you create in a file. They are just subroutines that are in the same file, not following a specific order.

In a nested function, the functions are contained in one function known as the parent function. These are functions that are used by experts in Matlab. They are particularly useful if the functions use shared data. Additionally, it’s much easier to call a variable in the parent function without the need to pass it as an argument in the other function.

#### 4. Public and global.

Here we define a function depending on where it can be used. Private functions are used in the file where they are created while public functions can be used in other files.

**Is it possible to use Matlab for mathematics?**

The answer to this question is a straightforward one. Yes, it’s possible. Matlab is a mathematical computing software that has been in the industry for more than three decades. It has evolved to become one of the core programming languages. Unlike other high-level programming languages such as C, Matlab was introduced in the market to help with mathematics problems. This was its goal and the software has never deviated from it. As time went by, it has been incorporated into the curriculum of many universities.

The main advantage that you will find with Matlab when developing a mathematics code is that it has all the functions that you will need. In most cases, you won’t need user-defined functions. But if you do, you will find the process of debugging easier as compared to other languages.

**What mathematical problems can I solve using Matlab?**

There are a lot of Matlab functions that you can use for mathematics. But mathematics is quite wide ranging and is used in most disciplines. In fact, everything is built upon mathematics. All the engineering tasks has mathematics in it. Here are some of the topics where you can use Matlab: –

Fourier transform

Laplace transform

Regression analysis

Arithmetic operations

Algebra

Calculus

Number theory

Numerical differentiation

Probability theory

Computational geometry

Signal processing

Computational finance

**Why we are the best**

Some time students might find it hard to complete a mathematics assignment. They could be short of time or may have lots of assignments to attend to with strict deadlines. In some cases, the assignment might be tough and need an attention of a professional. Students, therefore, result in asking for professional help which ensure the completion of the assignment before the deadline.This also ensures that the students score high marks. Matlab assignment experts is onesuch academic help platform.

We are a platform that has been serving students for years. We offer our servicesfor anykind of Matlab related assignment. For the years we have been in this business, we have provided our clients with the finest services. As a result of this, we have amassed a good number of loyal clients pleased with our services.

Offering the best services is in our DNA. Any sort of negligence is not overlooked in our platform. We do not allow late submissions, plagiarized work or giving you a low-quality assignment solution. This has enabled us to remain one of the mostpreferred online assistance companies.

Our dedicated pool of experts ensure that assignment solutions are delivered as planned. They are the ones who are the implementersof our stringent policies.When we hire our experts, we ensure that they are highly qualified and experienced in this domain. This sets us above our competitors. Do you want our services? Contact us, and you, too, could be in the list of satisfied students who have given us a five-star rating. Simply click on the ‘submit your assignment’ button on our webpage to enjoy our top-class service. We promise you that we will help you score a high grade, irrespective of howcomplex you think your assignment is.

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)’);