2.1. Write a simple MATLAB program which will calculate sum of the first n integer numbers.
Do not use neither the wellknown analytical formulanor the builtin MATLAB sum function for solving this task.
Test your code for n100 and compare with the formula and builtin sum function figure out the syntax in MATLAB help page for the usage.
For full mark:Test your code for different values of n and compare it with formula if you wish to be fully certain that your code works. Make sure you protect your code from illogical use n needs to be a positive integer.
5 marks
2.2. Similarly, write a MATLAB program which calculates sum of all elements in array b which needs to be provided by user use input command.
Do not use builtin MATLAB sum function for solving this task.
Test the program with array 5 12 33 45 22 28 125 14 35 and compare the result with MATLABssum function.
For full mark:
Test your code for random array with 10, 50 and 500 elements and compare it with MATLABs sum function.
Hint: Explore MATLAB help to figure out how to make random array of n elements.
5 marks
Note: Do not write code that works only for the test example, if the text suggests general input, explore input command in MATLAB help.
This flow chart can be used to find the real roots of a quadratic equation of the form
ax2bxc0.
3.Construct MATLAB program which will solve roots x1 and x2 for any set of input variables a,b,c anddemonstrate how your program works for specific examples:
6x25x110
x26x90
4x23x80
4x212x90
6x213x50
3x28x50
For full mark:
Add option to plot ax2bxc in your code, and copy paste plots for each example in your reporttry making neat graphs, i.e center x axis around the extrema of the function at xb2a so that the roots if existing are visible.
Make the code userfriendly, check MATLABs input command.
15 marks
The equation 2x3x2150 may be solved by iteration using the flow chart given below:
4.Write MATLAB program which implements the algorithm above, use 13 for powering the expression and answer following tasks:
4.1Use a2 to find a root of the equation
4.2Use a20 to find the root of the equation. What do you notice? Double click on variable b in the MATLAB workspace
4.3Use nthrootexpression,3 function look MATLAB help instead of expression13 andrepeat 4.1 and 4.2. What do you notice now? Why did this happen?
4.4Repeat the code for a1, 1.8, 2, 5, 10, 50, 500. What do you notice? What does parameter a represent?
20 marks
5.Fibonacci sequence is an array that forms the next entry by summing the previous two: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 . . .
Write a MATLAB program that takes positive integer number N as an input, and outputs the Nth number in Fibonacci sequence. Do not use MATLABs builtin function fibonacci for solving thetask.
Test the program for N5,10,50,150 and compare your results with fibonacciN in MATLAB.
For full marks:
Print first 40 elements of Fibonacci sequence and include it in your report.
Make sure you protect your code from illogical use N needs to be a positive integer.
Create a flow chart for your code.
Hint:In a for loop, in iteration i you can access the previous value of the array a as ai1, or even further as ai2. You can start iterating through array from any element, for i7:lengtharray will access array7 first if you have arrayi in the loop somewhere.
NOTE: Read the task very carefully, we want Nth element of Fibonacci sequence not the sequence up to N!
In sequence up to 90:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
the 11th element is 89, the fifth is 5 and so on.
10 marks
6.A number is called prime if it is only dividable by 1 and itself. Number 1 is not considered prime.
Write a MATLAB program that takes positive integer number N as an input, and outputs the Nthprime number in prime number sequence. To check divisibility you can use MATLABs ymodA,Bfunction which gives output y0 if A can be divided by B Check help page for more info.
The simplest algorithm requires you to find a root of A, round it to the nearest integer number x, andthencheck if A is dividableby any of the numbers in range 2,3,4,..,x.
Do not use MATLABs primesN or isprimeN functions for solving the task.
Test the program for N5,10,50,150 and check if your output is prime with MATLABs isprimeoutput function..
For full marks:
Print first 40 prime numbers and include them in your report.
Make sure you protect your code from illogical use N needs to be a positive integer.
Create a flow chart for your code.
Conserve your memory usage, once you found root of A, your range up to x does not need to include any even numbers, and you can therefor cut the size of array by half!
NOTE: Read the task very carefully, we want Nth element of prime number sequence not the sequence up to N! Note that number 1 is not considered prime.
In prime number sequence up to 40:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37
the 5th element is 11, the 10this 29 and so on.
20 marks
Reviews
There are no reviews yet.