Skip to main content

Posts

Showing posts from December, 2015

Third side of a triangle if two sides and the angle is given using Matlab

Problem Write a program in Matlab to ask the user with the length of the two sides and angle between two sides. Then use the following relation to calculate the third side of the triangle: Equation to calculate the third side of the triangle if two sides and angle is given Solution We can solve this right hand side of the equation to get the value of the third side. But we will have to write the above equation to matlab equivalent. It is very easy and we will solve using functions. I suggest you refer to program section and then refer to the explanation section to get a detailed explanation of the same. Some of the concepts you need to know to solve the problem are as follows: 1) Matlab functions 2) Operator Precedence 3) input function  4) Matlab Variables 5) fprintf in Matlab Program 1 This is functional approach 1 2 3 4 5 function size = third_side_function (a,b,theta) %third_side(a,b,theta) will give the length of third side of triangle size ...

Factorial of a number using Stirling's(Gosper) Approximation using Matlab

Problem Find the factorial of a number using Modified Stirling's formula(Gospers). Also print the difference upto 5 decimals between the result from the factorial function in Matlab and the result you get from approximations.The relation to find the factorial of a number using the Stirling's formula is given by: Gospers formula(Modified Stirling formula) To know more about Stirling's formula or Gospers formula then go to: Stirling's Approximation - Math.Wolfram Solution We will solve this problem using Matlab functions . This is similar to our previous post Velocity of a moving fluid using Matlab . But the little difference between the previous post and this post is that we are required to print the difference upto five decimals of the difference between the factorial function in matlab and the above formula. It should be remembered that we are asked indirectly to print the modulus of the difference. To know about modulus of a number then please do check...

Velocity of a moving fluid using Matlab

Program The velocity of a moving fluid can be found by calculating the difference between the total and static pressure P t and P s . For water, this is given by Velocity of a moving fluid formula for water Where, P t  is Total pressure P s is Static Pressure. Write a function that will receive as input arguments the total and static pressures and will return the velocity of the water. Solution This problem has been taken from the book A practical introduction to Matlab programming by Stormy Attaway . I suggest you to read this book if you want to become a professional without any pain. Coming to solving the question we have already solved a similar question here: Total Amount after n years - bank interest calculation using Matlab .  If you have read the above post then you will be able to solve this problem directly if you are able to write the Matlab equivalent of the above equation. As this program is direct to solve and there is no complicated thing ...

Total Amount after n years - bank interest calculation using Matlab

Problem If a certain amount of money (called the principal P ) is invested in a bank account, earning an interest rate i compounded annually, the total amount of money Tn that will be in the account after n years is given by: T n = P(1+i) n Write a function that will receive input arguments for P, i, and n, and will return the total amount of money T n . Also, give an example of calling the function. Solution The above problem has been taken from the book Practical introduction to Matlab Programming by Stormy Attway . This is a very good book and a must read for every matlab programmer who wants to become a professional. This book is also for beginners if you want to start from scratch and become a professional. I think the question is very explanatory by itself so that I don't have to give you explanation about the same. If you want to know more about the money deposited(principal) , interest , total amount then you can go here: Interest rate - Wikipedia . This pr...

Draw graph for exp, sin, cos, tan using Matlab

Program Write a program to draw the graphs for exponential function(exp) , and trigonometric functions - sine function(sin) , cosine function(cos) , tangent function(tan) using graph. Use the plot function in Matlab. Solution  There are two ways to solve this problem. While there are many ways in programming to solve a problem but I am stating only two because they are major. They are  1) Plotting all the above functions in different windows - Plot function . 2) Using subplot and projecting the graphs in a single window - subplot function . While for the sake of simplicity I will divide this post into two sections where there will be program 1 and explanation 1 corresponding to program 1 and program 2 - explanation 2 corresponding to program 2.  Even if you are beginner then first refer to the first program and then go refer the program 2 so that you will get to know the difference between what plot and subplot is. Also there is a detailed explanation fo...

Convert from miles per hour to kilometers per hour and vice versa using Matlab

Problem Write a Matlab program to convert from miles per hour(mph) to kilometers per hour(kph) if the user gives the input in mph and from kph to mph if the user gives input in kph. (Hint: Ask the user to enter the value first and then ask the user to select the units giving options.). The conversion factors are as follows: Miles per hour(mph) to Kilometers per hour(kph) conversion Kilometers per hour(kph) to Miles per hour(mph) conversion You can get to know about the speed and its units Speed - Wikipedia . Solution This problem is similar to our previous post: Convert Temperatures Celsius Fahrenheit using Matlab . Hint has also been given in the question itself and I suggest you to directly see program section and for detailed explanation then see explanation section. Some of the concepts you need to know to solve this problem are as follows: 1) Request User input Matlab 2) fprintf function in Matlab 3) variables in Matlab 4) if elseif else statements in Ma...

Volume of a solid sphere and graph for different radius using Matlab

Program Calculate the volume of a solid sphere based on the user input given and also plot a graph for different values of radius with an interval of 0.5 and range of [r-5,r+5]. The relation for the volume of the sphere is given by: Volume of a sphere Where r is the radius of the sphere You can learn more about sphere from here:  1) Sphere - mathsisfun.com 2) Sphere- Wolfram Solution First of all let us understand the question before go into solving the problem using Matlab . 1) The question says that we will have to calculate the volume of a sphere by taking the input of the value of the radius from the user. 2) The question also asks us to draw the graph for a given set of range with a given set of interval . So the question asks us to take radius on x axis and volume on y axis . Next the difference between two points on the x axis should be 0.5 as said by the interval and finally we will have to draw the graph in the range [r-5,r+5]. Lets understand th...

save append and read data from a file using Matlab

Problem Create a 4x4 random matrix and save it in a file. Now create a 4x4 matrix and append the new matrix to the file. Finally read the data from the file and display on the command window. Solution In this program we will create a matix using the rand function and then save it to the file. Again we will create a matrix and append the matrix to the file. Finally we will read the data from the file. In this program we will only use the command prompt window to accomplish the task. First of all head on to the program section to get insight into what is happening, then you can refer to the explanation section to get detailed explanation of the program section. Some of the concepts used to solve this post are as follows: 1) Matrices in Matlab 2) rand function in Matlab 3) Save in Matlab 4) Append in Matlab 5) Load in matlab 6) variables in matlab 7) matrix In Mathematics Program >> mat = rand ( 4 , 4 ) mat = 0.7094 0.6551 0.9597 0.7513 ...