+1 (315) 557-6473 

Matlab Code for Mathematics Problems Assignment Help

Matlab code for mathematics problems

Developing a Matlab code for mathematics problems is quite challenging, but sometimes it can be very easy. It all depends on the level of experience you have with the software. As a novice in using the software, you will definitely find the process very convoluted. Having a sound knowledge of Matlab makes things a little easier. If you are relatively new to Matlab, you can ask for our help and we will help you develop the code. In this article, we will help you get started with it so that you may not find the concepts behind creating a code very intimidating. However, we won’t delve very deep into developing the code, but this will serve as a guide in developing code in Matlab.


Matlab or matrix laboratory is a high-level mathematical computing software developed by Mathworks a few decades ago for matrix calculations. But now, its use is very extensive. It can be used to solve any mathematical task with just a few lines of code. One can use it with great dexterity only when he/she has mastered how to use it. One good aspect of the software is that it is well documented and has a community where one can ask questions.

Why should you use Matlab?

As a beginner, you might be wondering why should I use Matlab to develop the code to solve mathematical problems. Whereas, it could be solved much easier by using Microsoft Excel or SPSS. There are so many advantages of using it, here are some of them.
  1. The process of debugging is made easy. Comparing it with other high-level programming languages would reveal that it’s much easier to debug a Matlab code as you can correct the errors as you move on.
  2. It has a scientific base. The science community endorses the use of the software in all its operations. In fact, most institutions around the world have incorporated it into their curriculum.

Matlab basics

If someone told you that you could master how to use a Matlab program within a day, then they are wrong. To master the software, you need a lot of dedication and hard work. Let’s not forget that you will have to sacrifice one of your movie nights only to train how to use it. But in the end, it’s worth it. You will be able to do a lot of things with the software that you never thought you would.
Starting to learn it will require you to grasp the basics. The basics will require you to learn the different types of data types, which include matrices and vectors. You will also learn to do basic mathematics operations and draw some of the basic plots. Additionally, you will learn how to import data from various databases and from Microsoft office. The basics are fairly easy to comprehend. Some would like to ignore it, but remember, without the basics, you might make some silly mistakes.
With time, you will be able to create your own custom code which you could reuse later for some types of tasks. There is a lot to learn here. You should brace yourself with this before beginning any course.

The process of developing code

Assuming you have learned the basics and are fairly knowledgeable in the program, you can create your own code with Matlab for a specific mathematics question. There is a systematic way of creating code for each assignment on a topic that we at Matlab assignment experts will teach you. Follow the following steps if you want to write high-quality code with zero errors.
  1. Read the question and understand its requirements. You should take some time here as without understanding the requirement of the problem, you might answer the question erroneously. . Take some time to research the question at hand.
  2. Research on the solution to the question. When you arrive at the algorithm that can be used to answer the question, contemplate on it and think about its suitability to the problem at hand.
  3. Now, you have to put the algorithm into a coded form. You have to find the appropriate functions to be used from Matlab documentation in answering this question.
  4. Write the code and ensure that everything is correct. Save the code and run it. If it does not function according to the way you want it, debug it and re-run the code.
  5. Once it functions as per your requirements, it will produce outputs such as graphs and tables or a value. You can save the graphs and copy them to an appropriate document file.

An example code

In this example, we will take the example of a log-rank test. First, we start by discovering what it does. From the research that we conducted, the log-rank test is a survival analysis hypothesis test. Mostly, it’s used when there is censoring. Censoring in survival mathematics implies that the individuals did not undertake the study until it was completed. They either died or quit the study. A sample can be right, left, or interval-censored. Right censoring and left censoring are the most common form of censoring. The log-rank test is more suited for right-censored data. Let’s apply the above procedure in writing code.
The first step is to read and understand the requirement of the question. Here we assume that you are using your own survival data. For a log-rank test, we test if the survival distribution in two independent groups is equal. Our null hypothesis is that the survival distributions in the two groups are equal, and the alternative hypothesis is that the survival distributions in the two groups are not equal.
The second step is to think of the solution that we want. Here we check for the p-value, whose significance level will be given in your assignment. If it’s not given we always use a significant level of 0.05. For the solution, we check for the p-values. A p-value of fewer than 0.05 means that we go with the alternative hypothesis while a large value means that we go with the null hypothesis. We shall also need a stepwise line graph for the two groups plotted on the same plot.
The third step is to find out how we can implement it in the software. Is there a function that we can use to find the values directly, or we could develop our own manual code? In some cases, your professor might require you to have a ready-made function. In this case, we only need to find a function for the log-rank test. Upon researching from different sources, we discovered that there is a log-rank function that can give us a p-value and the stepwise plot. Now we go to step four.
We write the code. First, we have to load the dataset for analysis. Once we have the dataset, we use the ready-made function log-rank (x, y alpha) where x and y are the two groups, and alpha is the significance level. We save the code and run. If it does not produce the intended function, we debug it until it produces the result. Errors could be typographical. Just ensure everything functions as it is supposed to.
Finally, if everything is good, save the graphs and the p-value and continue with your report.
Students who find it tough to develop a Matlab code for mathematics problems should contact us for professional help. We have been helping students with Matlab related assignments for close to a decade. You, too, should contact us to enjoy our reliable services.
The sample solution provided here is for a set of five questions. The brief of questions, the solution to which has been demonstrated by the expert, is summarized as follows: –
Q 1. The expert has written a Matlab code using if-else statements that implements a given function.
Q 2. The expression for friction factor as a function of Reynolds number is given for incompressible flow in a smooth pipe. The expert has demonstrated by writing the Matlab code for friction factor for some given values of Reynolds number.
Q 3&4. The power series for π is given in the question. Matlab code has been demonstrated by the expert using the “while” loop and using the “for” loop in two separate answers for the given condition.
Q 5. Data set is given, the expert has showcased a Matlab code using “for” loop for finding out statistical values like Harmonic mean, Geometric mean, Root Mean Square average.
clc, clear all, close all
%% Question 1
fprintf(‘%%%%%%%% QUESTION 1 %%%%%%%%\n\n’);
x = [8; 4; 3; 9; 0; -5]; % Define the data
n = length(x); % number of elements
f_x = zeros(n, 1); % Vector to store the results
k = 1;
for i = 1:n % iterate through all the value of x
xi = x(i);
if xi <= 1 % x <= 1
f_x(k) = 0;
elseif xi > 1 && xi < 5 % 1 < x < 5
f_x(k) = (xi-1)^2;
elseif xi >= 5 % x >= 5
f_x(k) = 16;
k = k + 1;
Results_Q1 = table(x, f_x) % Create table with results
%% Question 2
fprintf(‘\n%%%%%%%% QUESTION 2 %%%%%%%%\n\n’);
Re = [3e6; 4e4; 9e8; 3500; 1800; 500]; % Data
n = length(Re); % Number of elements
f_Re = zeros(n,1); % Vector to store the results
k = 1;
for i = 1:n % iterate through all the values
Rei = Re(i);
if Rei <= 2100 % Re <= 2100
f_Re(k) = 16/Rei;
elseif Rei > 2100 && Rei < 10^5 % 2100 < Re < 10^5
f_Re(k) = 0.0791/(Rei^0.25);
elseif Rei >= 10^5 % Re >= 10^5
f_Re(k) = 0.004;
k = k + 1;
Results_Q2 = table(Re, f_Re) % Create table with results
%% Question 3
fprintf(‘\n%%%%%%%% QUESTION 3 %%%%%%%%\n\n’);
e = 10^(-8); % epsilon (min error)
error = 1e50; % initial error
k = 0;
pi_calc = 0; % variable to store the calculated value for pi
sumvar = 0; % Variable to store the sum of the terms
while(error > e)
term = ((-3)^(-k))/(2*k+1); % current term of the sum
sumvar = sumvar + term; % Update the value of the sum
pi_calc = sqrt(12)*sumvar; % Current value of pi calculated
error = abs(term/pi_calc); % calculate current error
k = k + 1;
k = k – 1;
fprintf(‘After %s iterations, the value of pi calculated is: %s\n’, num2str(k), num2str(pi_calc));
%% Question 4
fprintf(‘\n%%%%%%%% QUESTION 4 %%%%%%%%\n\n’);
e = 10^(-8); % epsilon (min error)
error = 1e50; % initial error
pi_calc = 0; % variable to store the calculated value for pi
sumvar = 0; % Variable to store the sum of the terms
n = 1e6; % Max number of terms to consider. This value is reached only if the condition error < e is never met
for k = 0:n
term = ((-3)^(-k))/(2*k+1); % current term
sumvar = sumvar + term; % Update the value of the sum
pi_calc = sqrt(12)*sumvar; % Current value of pi calculated
error = abs(term/pi_calc);
if error < e % if current error is less than e (10^-8) then break the loop
fprintf(‘After %s iterations, the value of pi calculated is: %s\n’, num2str(k), num2str(pi_calc));
%% Question 5
fprintf(‘\n%%%%%%%% QUESTION 5 %%%%%%%%\n\n’);
DATA = [92.3, 93.2, 91.9, 93.5, 92.7, 93.1, 93.8, 92.4]; % Define the data
n = length(DATA);
%% PART a
fprintf(‘ Part a)\n’);
sum_inv = 0;
for i = 1:n
xi = DATA(i);
sum_inv = sum_inv + 1/xi; % Calculate the sum of the inverse of the values (1/x1 + 1/x2 + … + 1/xn)
Hm = n/sum_inv; % Formula for Hm
fprintf(‘The value of Hm is: %s\n\n’, num2str(Hm));
%% Part b
fprintf(‘ Part b)\n’);
product_vals = 1; % Variable to store the multiplication of all data values
for i = 1:n
xi = DATA(i);
product_vals = product_vals*xi; % Calculate the product of all the values (x1*x2*…*xn)
Gm = product_vals^(1/n); % Formula for Gm
fprintf(‘The value of Gm is: %s\n\n’, num2str(Gm));
%% Part c
fprintf(‘ Part c)\n’);
sum_sq = 0; % Variable to store the sum of the squares of all the values
for i = 1:n
xi = DATA(i);
sum_sq = sum_sq + xi^2; % Calculate the sum of the squares (x1^2 + x2^2 + … + xn^2)
RMSa = sqrt(sum_sq/n); % Formula for RMSa
fprintf(‘The value of RMSa is: %s\n’, num2str(RMSa));
Related Reviews