clear all; close all; clc;
x = input('x= ');
y = input('y= ');
if (x>=5) & (y>2)
f(x,y)=x^2-y;
elseif (x<5) | (y>0 & y<=2)
f(x,y) = x-y^2;
else (x<0 & x>0) & (y<0)
f(x,y)= x^3 + y^3;
end
f(x,y)
%when I try input x =0 and y=2 it does not run
%Index in position 1 is invalid. Array indices must be positive integers or logical values.
%How to I solve that?
%Thank you very much

댓글 수: 1

clear all; close all; clc;
x = 0 ;
y = 2 ;
if (x>=5) & (y>2)
f = @(x,y) x^2-y; % define function using function handle
f(x,y)
elseif (x<5) | (y>0 & y<=2)
f= @(x,y) x-y^2;
f(x,y)
else (x<0 & x>0) & (y<0)
f = @(x,y) x^3 + y^3;
f(x,y)
end
ans = -4
Define the function using its function handle as the way you try to call function

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

 채택된 답변

Matt J
Matt J 2023년 1월 2일
편집: Matt J 2023년 1월 2일

0 개 추천

x=0; y=2;
if (x>=5) & (y>2)
f=x^2-y;
elseif (x<5) | (y>0 & y<=2)
f = x-y^2;
else (x<0 & x>0) & (y<0)
f= x^3 + y^3;
end
f
f = -4

댓글 수: 2

Vo
Vo 2023년 1월 2일
why we use f due to f(x,y) sir ?
Matt J
Matt J 2023년 1월 2일
편집: Matt J 2023년 1월 2일
Because, "array indices must be positive integers or logical values"
For example:
A=rand(1,4)
A = 1×4
0.8551 0.1501 0.7576 0.0665
We can do
A(1)
ans = 0.8551
A(2)
ans = 0.1501
but not,
A(0)
Array indices must be positive integers or logical values.
There is no location in the vector known as A(0).

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2022a

태그

질문:

Vo
2023년 1월 2일

편집:

2023년 1월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by