MATH/CMSC 206 - Introduction to Matlab
Announcements | Syllabus | Tutorial | Projects | Submitting |
Answers to Self-Test
1. The following will do the job. Notice that if a longer vector is passed only the first two entries will be added:
function y=add(x) y=x(1)+x(2); end; |
It's worth commenting that we could have a more sophisticated version which checks the argument and if it's not a vector an error is produced. Something like this:
function y=add(x) if (isvector(x)) y=x(1)+x(2); else disp(['Your argument ',char(x),' is not a vector!']); end; |
2. We have to dig into our calculus knowledge for the formula. We'll do ours in meters per second:
function m=maxheight(v,h) m = -4.9*(v/9.8)^2+v*(v/9.8)+h; end; |
3. This one will include an integral. Note that we could have done the integral ahead of time to create a formula:
function a=area(b) a = int('1/x',1,b) end; |