필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

error when substituting value

조회 수: 1 (최근 30일)
Hao Ming Low
Hao Ming Low 2020년 3월 24일
마감: MATLAB Answer Bot 2021년 8월 20일
what is the meaning of the code below?
>> f(x) = -0.5.*(x.^2)+2.5*x+4.5
x = 4
f =
0 0 0 0 4.5000 1.5000 -2.5000 -7.5000 -13.5000 -20.5000
x =
4
i typed the quadratic equation and wish to find the value from 0<x<20 ,but the table emerges. can anyone explain the table for me?

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 3월 24일
Hao - your code above code is creating an array (this is your f) and not a table. So when you say something like
x = 3;
f(x) = -0.5.*(x.^2)+2.5*x+4.5;
you are setting the third element to the evaluation of -0.5.*(x.^2)+2.5*x+4.5 for x=3. If you have not previously assigned anything to the first or second elements, then they are set to zero. I'm not sure if that is your intent, but an alternative approach might be to use an anonymous function for your quadratic:
f = @(x)-0.5.*(x.^2)+2.5*x+4.5;
x = 1:1:19;
result = f(x);
where result is an array where the kth element is f(k). (Note that is only true when k is an integer - if the x data contains non-integer values, then the kth element of the results array would correspond to f(x(k)).)
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2020년 3월 24일
Hao - all you need is the three lines. I'm not sure what you mean by nothing appear. It could be that semi-colon at the end of the third line is suppressing output that you are expecting to see. Either remove the semi-colon or just do
f = @(x)-0.5.*(x.^2)+2.5*x+4.5;
x = 1:1:19;
result = f(x);
result
to see what result contains.
Hao Ming Low
Hao Ming Low 2020년 3월 24일
thank you!!! it works after semicolon removed

Community Treasure Hunt

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

Start Hunting!

Translated by