1) Answer the following questions in your logbook: 1. 2. 3, 4. 5. 6. Describe a
ID: 2073655 • Letter: 1
Question
1) Answer the following questions in your logbook: 1. 2. 3, 4. 5. 6. Describe a specific use for a loop. Write a loop that computes the average of the values in a vector. (l know there are easier ways to do it, e.g. the MATLAB mean() function. Do it the long way.) what does the MATLAB function “mod" do? What is the difference between elseif and else? What are some practices you can use when writing code to make it clear to the user? What can you do to your code to make it easy to debug and modify in the future? 2) Complete the following: Write a MATLAB function that takes an input vector, loops through the vector to find the biggest value, and returns the location in the vector of that value. For example, when given x=[3 4 8 2 9 2], the function should return 5 Write a MATLAB function that computes and returns the least common multiple of two input values. . .Explanation / Answer
Ans 1) 1.
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times with specific variable value gets update for each loop.
syntax of a for loop in MATLAB is as following
for index = values
<Program Statements>
....
end
Example
here number of time loop executed depends upon number of element specified for V (i.e. 4 times ) and value of V gets update as 1,5,8,17 for each loop
so ouput of this code is
1
5
8
17
1) .2 .
% For Loop
x= [1.8 3.6 5.4 7.2];
average = 0; % Initialize
for k = 1 : length(x)
average = (average *(k-1) ) + x(k);
average = (average)/k;
end
average
output : - 4.5
1) 3.
b = mod(a,m) returns the remainder after division of a by m, where a is the dividend and m is the divisor.
example .
output :- 3
1) 4 .
An if statement can be followed by one (or more) optional elseif... and an else statement, which is very useful to test various conditions
When using if... elseif...else statements, there are few points to keep in mind
1.An if can have zero or one else's and it must come after any elseif's.
2.An if can have zero to many elseif's and they must come before the else.
3.Once an elseif succeeds, none of the remaining elseif's or else's will be tested.
Syntax for if, elseif, else
difference between elseif and else is elseif get comes with expression (like if ) if expressionof elseif evaluate to be true then statements under elseif get executes and none of the remaining elseif's or else's will be tested. ok but above all expreesion for if and elseif evaluate to be false then statements under else get executes. so it is clear that else its like default (i.e. if all expression evalaute as flase then else shlould get execute without any confusion),else never comes with expreesiion.
1) 5.
1)6.
We should add comment for critical portion which can expalin the alogorithm of our code, which is going to help us in future modification of code.
Try code folding. Keep only the code that you are working on unfolded. That would improve the code readability, might also require less java memory. Also divide your code into code sections and debug one section at a time rather than the entire file.
2)1.
function k = maxloc(a)
mx=a(1);
k=1;
for p=2:numel(a)
2) 2. suggested code if you find better go for other for this code you required to use function gcd
function c = mylcm(a, b)
% The least common multiple c of two integers a and b.
if feval('isint',a) & feval('isint',b)
c = a.*b./gcd(a,b);
else
error('Input arguments must be integral numbers')
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.