Array indices must be positive integers or logical values

Hey guys,
I'm trying to complete a homeowrk problem and I keep running into the error "Array indices must be positive integers or logical values." I don't know what I am doing wrong since my professor told us how to do most of the problem. Any help would be greatly appreciated. Thank you!
clear;clc
x = [0.888: 0.01: 1.178]; %x values
for n = 1: 1: length(x)
%must have different vector because variable index must be an integer
f(x) = -5*x(n)^3+17*x(n)^2+8*x(n)+9; %first function
g(x) = -13*x(n)^3+16*x(n)^2+9*x(n)+18; %secon function
if f(x) == g(x)
%if the two functions outputs equal each other then they intersect
disp(x)
%displays the x value of the intersection
end
end
figure(1)
plot(x, f(x), x, g(x))
%creates a plot with two lines on the y-axis
title('WA06B by Tiffany Tabor')
xlabel('x values')
ylabel('f(x) and g(x)')

 채택된 답변

Alan Stevens
Alan Stevens 2020년 10월 3일
The following works
x = [0.888: 0.01: 1.178]; %x values
for n = 1: 1: length(x)
%must have different vector because variable index must be an integer
f(n) = -5*x(n)^3+17*x(n)^2+8*x(n)+9; %first function %%%%%%%%% f(n) not f(x)
g(n) = -13*x(n)^3+16*x(n)^2+9*x(n)+18; %secon function %%%%%%%%%%% g(n) not g(x)
if f(n) == g(n)
%if the two functions outputs equal each other then they intersect
disp(x)
%displays the x value of the intersection
end
end
figure(1)
plot(x, f, x, g) %%%%%%%%%%% Just f and g
%creates a plot with two lines on the y-axis
title('WA06B by Tiffany Tabor')
xlabel('x values')
ylabel('f(x) and g(x)')

추가 답변 (2개)

Star Strider
Star Strider 2020년 10월 3일

0 개 추천

See the documentation sections on Anonymous Functions and also Array vs. Matrix Operations.
You will need to understand those in order to complete this.
Image Analyst
Image Analyst 2020년 10월 3일

0 개 추천

This is one of the most FAQed of the FAQs, so see the FAQ for a thorough discussion.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 10월 3일

답변:

2020년 10월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by