필터 지우기
필터 지우기

I am a beginner and this is just a short question. How can I input the matrice x=[-1 0 5 10] into an if statement and use the output to calculate Y?

조회 수: 1 (최근 30일)
Write a script to do the following
Read a value of a scalar
Then compute & display the value of using the function [y=3x^3-0.9x+5] in case that in range of . If "x" not in this range 0<x<=10, "y" will not be calculated nor displayed.
y=3x^3-0.9x+5 for 0<x<=10
Test the script using the following cases of "x" and then summarize if "y" will be displayed or not .
(a) x = -1
(b) x = 0
(c) x = 5
(d) x = 10
  댓글 수: 2
Myo Min
Myo Min 2022년 11월 22일
I used
x=[-1 0 5 10]
y=3*x.^3-0.9*x+5
if x>0 && x<=10
disp(y)
else
disp("Out of range")
end
And it is not working. When I try to find other answers, I don't understand the other commands.

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

채택된 답변

Image Analyst
Image Analyst 2022년 11월 22일
You need to loop over all x if you have x in a vector like that
x = [-1, 0, 5, 10];
for k = 1 : numel(x)
if x(k) > 0 && x(k) <= 10
y = 3 * x(k) .^ 3 - 0.9 * x(k) + 5;
fprintf('y(%d) = %f\n', x(k), y);
else
fprintf("Your x of %.1f is out of range.\n", x(k))
end
end
Your x of -1.0 is out of range. Your x of 0.0 is out of range.
y(5) = 375.500000 y(10) = 2996.000000

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 11월 22일
Hint:
x = input(
if x > 0 && x <= 10
%code
else
%code
end
I'm confident you will be able to figure out the last few characters to complete the assignment.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by