How can I write the MATLAB code f(x) = x + 5 for 0<x<10?

81 views (last 30 days)
The following code did not solve the problem.
if ((0 < x) | (x < 100)) y = x + 5 end

Answers (4)

Walter Roberson
Walter Roberson on 10 Oct 2013
You cannot define that in MATLAB: it is not possible to define a function that has no definition over a particular range of arguments. You can define a function that is NaN outside a range, or 0, but you have to give it some meaning. Either that or never call the function with parameters outside the defined range.

sixwwwwww
sixwwwwww on 10 Oct 2013
There is a work around to this problem using symbols in MATLAB as follows:
Suppose your x value lies over range from -100 to 100 and for range 0 to 10 you need y = x + 5, then you can do as follows:
syms x_sym y_sym
for x = -100:1:100
if (x > 0 && x < 10)
y_sym = x_sym + 5;
% Other processing as desired. Also result can be back converted from symbol type to
% double number type as
% y = double(subs(y_sym, x_sym, x));
end
end
I hope it will help you a little
  5 Comments
Walter Roberson
Walter Roberson on 4 Jan 2017
count is acting as an index into the vector being constructed. You do not know ahead of time how many values will match.

Sign in to comment.


Big Wolf
Big Wolf on 6 Jun 2020
Write a MATLAB program to find the value of y when y=|x| when x<5 and y=5x when 5<or = x <10
  1 Comment
Walter Roberson
Walter Roberson on 8 Jun 2020
If x<5 but also 5<or=x<10 then x<5 but also 5<=x which is x >= 5. There is, however, not number which is simultaneously less than 5 and >= 5. Therefore the problem is impossible.

Sign in to comment.


abod
abod on 12 Feb 2023
if r=10 and the i increased from 0 to 10 with increments of 2 and v=i*r

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!