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

답변 (4개)

Walter Roberson
Walter Roberson 2013년 10월 10일

0 개 추천

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 2013년 10월 10일

0 개 추천

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

Sam
Sam 2013년 10월 10일
Thanks!
Sam
Sam 2013년 10월 10일
The program as written results in a plot with one number, 10. How can the code be modified to produce a range of numbers along the x axis between but not including digits 0 and 10?
In this case the code can be written in the following form:
syms x_sym y_sym
count = 1;
for x = -100:1:100
if (x > 0 && x < 10)
y_sym = x_sym + 5;
y_range(count) = double(subs(y_sym, x_sym, x));
x_range(count) = x;
count = count + 1;
end
end
disp('x values between 0 and 10')
disp(x_range)
disp('Values for function y = x + 5 given x values')
disp(y_range)
what is the purpose of count here?
count is acting as an index into the vector being constructed. You do not know ahead of time how many values will match.

댓글을 달려면 로그인하십시오.

Big Wolf
Big Wolf 2020년 6월 6일

0 개 추천

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

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.

댓글을 달려면 로그인하십시오.

abod
abod 2023년 2월 12일

0 개 추천

if r=10 and the i increased from 0 to 10 with increments of 2 and v=i*r

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

Sam
2013년 10월 10일

댓글:

2023년 2월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by