Question

3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested,

0 0
Add a comment Improve this question Transcribed image text
Answer #1

3.

% Matlab function that takes a 3-element vector and returns three-scalar
% output arguments in non-decreasing order.
% first = smallest element of data
% last = largest element of data
function [first, second, last] = sort3(data)
if(data(1) < data(2))
if(data(1) < data(3))
first = data(1);
if(data(3) < data(2))
second = data(3);
last = data(2);
else
second = data(2);
last = data(3);
end
else
first = data(3);
second = data(1);
last = data(2);
end
else
if(data(2) < data(3))
first = data(2);
if(data(1) < data(3))
second = data(1);
last = data(3);
else
second = data(3);
last = data(1);
end
else
first = data(3);
second = data(2);
last = data(1);
end
end
end
%end of function

Output:

[first,middle, last]-sort3 ([2,3,11) first 1 middle- 2 last 3

4.

% Matlab function to calculate the value of the polynomial given x value
% and coefficients in vector c and constant value in c0
function p = poly_val(c0,c,x)
% if c is empty
if(isempty(c))
p = c0;
else
if(isscalar(c)) % if c is scalar
p = c0 + c*x;
else % if c is vector
p = c0;
for i =1:length(c)
p = p + (c(i)*(x.^i));
end
end
end
end
%end of function

Output:

>> poly val(5, [1,2) ans >>poly_val (5, 3,2) ans >> poly val (5, [3,2],2) ans 19

Add a comment
Know the answer?
Add Answer to:
3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-argumen...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT