Asked by Paul
on 26 Sep 2013

Hi, I'm a student studying engineering and I have my first exam tomorrow on MatLab. I'm going through some practice problems and ran into two that I don't understand, I posted them below and was wondering if anyone could help me out! Thanks

6) Assume that the function *diff* does not exist. Write your own expression(s) to accomplish the same thing for a vector.

7) Find expressions that will evaluate the function f of two variables x and y:

f(x,y) = 3*x - y

where x ranges from 1 to 2 and y ranges from 1 to 5.

*No products are associated with this question.*

Answer by Image Analyst
on 27 Sep 2013

Hint: type out vec(1:end-1) and vec(2:end) and see what that shows.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 3 Comments

## Cedric Wannaz (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/88381#comment_171102

For 6, build an numerical example, e.g. with a vector

if you understand what DIFF does, it is easy to emulate it with a simple loop (hint: it doesn't start on the first element).

For 7, you can certainly build two nested loops over elements of

xandyand build an array of solutions, but you could also consider an approach whereXandYare appropriate arrays of values ofxandy, and perform the evaluation offin one matrix equation. Look at the doc of MESHGRID to understand this comment.## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/88381#comment_171112

#6, diff() can be done without any loop.

#7 can be done without any loop and without any meshgrid (or ndgrid. or repmat)

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/88381#comment_171144

These problems are not complicated. So I wish you good luck for your first exam tomorrow.