Clear Filters
Clear Filters

Optimizing an equation point by point

3 views (last 30 days)
A on 23 Jan 2016
Commented: Walter Roberson on 23 Jan 2016
If I have an equation for a surface:
x = [0:10]; y = [0:10];
z = @(x,y) x + y;
[X,Y] = meshgrid(x,y);
SurfZ = z(X,Y);
Is there a way that I can 'change' the shape of this equation/surface by telling it to change it's Z value for a given [x,y] coordinates. So for example with the above equation, I'd like z value at x = 3 and y = 3 to equal 4 (instead of 6 as described by above mentioned equation.
Is this a possibility?
Thank you

Answers (1)

Walter Roberson
Walter Roberson on 23 Jan 2016
SurfZ(ismembertol(X,3), ismembertol(Y,3)) = 4;
ismembertol() is a relatively new function. If you have an older version of MATLAB then
tol = 1e-8; %for example
SurfZ( abs(X-3) < tol, abs(Y-3) < tol) = 4;
In the special case where your X and Y are guaranteed to be integers,
SurfZ(X==3, Y==3) = 4;
and of course if your X and Y all happen to be integers starting from 1, then
SurfZ(3, 3) = 4;
but in your particular case it would be
SurfZ(3+1, 3+1) = 4;
to account for the numbering starting at 0.
Once you have made the change to the matrix,
surf(X, Y, SurfZ)
Walter Roberson
Walter Roberson on 23 Jan 2016
SurfZ(X >= 3 & X <= 4, Y >= 3 & Y <= 4) = 4;
Walter Roberson
Walter Roberson on 23 Jan 2016
In order to modify the equation itself and still have it as an equation, you need to use one of the arithmetic forms of conditional expression. The most common operations used to express conditional expressions are: heaviside(), dirac(), and piecewise()
All three of those require the Symbolic Toolkit.
heaviside(x) is 0 if x < 0 and 1 if x > 0, and undefined if x == 0 exactly. (You can modify the value for x == 0 if you have R2015a or later Symbolic Toolbox, by using the new sympref() function.)
You would use heaviside(t-a) to express the condition t > a . Thus,
would be an expression with value A provided that t is in the range 3 to 4 exclusive (but watch out for t==3 or t==4 exactly)
dirac(x) is effectively 0 unless x is 0. You would use dirac(t-a) to express the condition t==a exactly. Thus,
would have the value A only when t is exactly 3.
More general is piecewise, which allows conditions and applicable formula to be written. There is no direct MATLAB interface to piecewise, so you need to reach into the symbolic engine to use it, and you have to be careful about how you construct the arguments. You can see some previous postings in which piecewise was constructed linked to below.
The symbolic engine knows how to reason about these three, can use solve() with them, can integrate and differentiate them. If you need those abilities, the above are you only options.
If you do not have the symbolic toolbox, then you will need to work numerically.
The easiest way to have conditional values numerically is to write a function (a real "function", not an anonymous function) which uses whatever "if" and so on that it needs to in order to compute the values.
In some situations, you can express conditional values in expressions by using the fact that true is 1 and false is 0. You can then take those things that would be expressed by heaviside and transform them to multiplication by logical values. For example,
which will be 0 if the value is false and will be A otherwise.
(x>=3 & x<=4).*A
can express that x is in the range 3 to 4.
z = @(x,y) (x>=3 & x<=4 & y>=3 & y<=4).*4 + (x<3 | x > 4 | y<3 | y>4).*(x + y);
This expresses "4 if these particular conditions are satisified, and (x+y) if these other conditions are satisfied, and 0 or empty otherwise"
x and y need to be the same shape for the above to work, but they can be vectors or grids.
More on using piecewise from MATLAB:

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!