What's the problem with this simple anonymous function?
조회 수: 2 (최근 30일)
이전 댓글 표시
Here's an simple code:
clear all; clc; close all;
n = 1:3;
for i = 1:numel(n) %numel is used if someother time I'll use negative values for n
f = @(x) x.^n;
fplot(f);
hold on;
f1 = @(x) x.^(1/n);
fplot(f1);
hold on;
end
The function "f" is running OK but "f1" is generating graphics error. For one value of n like:
f1 = @(x) x.^(1/2);
It generates the plot, but not in the loop.
Please let me know what's wrong?
댓글 수: 0
채택된 답변
Steven Lord
2021년 11월 10일
You likely meant to use n(i) in your functions, not n. Another option would be to use 1./n instead of 1/n.
n = 1:3
y = 1./n
z = 1/n(2)
w = 1/n
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!