How to plot a simple function that has a variable?

The question I have is:
Write a MATLAB function
func(x,a) = x^2 + ax - 6
Use the function to plot, func(x,1), func(x,2) and func(x,3) on the same axes, for -10<x<10.
Im really confused how to define a as 1, 2 and 3 and how to substitute them into the function.

 채택된 답변

madhan ravi
madhan ravi 2018년 11월 16일
편집: madhan ravi 2018년 11월 16일
Read about fplot()
syms x
for a=1:3
func = x^2 + a*x - 6 ;
fplot(func,[-10 10]) %ranging from -10 to 10
hold on
end
Screen Shot 2018-11-16 at 8.00.07 PM.png

댓글 수: 3

Thanks alot :)
Anytime :)
Stephen23
Stephen23 2018년 12월 9일
편집: Stephen23 2018년 12월 9일
I very much doubt that this assignment was intended to require the Symbolic Toolbox. The assignment "Write a MATLAB function ..." requested an actual MATLAB function:
and this is what the other answers provide.

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

추가 답변 (2개)

TADA
TADA 2018년 11월 16일
func = @(x,a) x^2 + a*x - 6;
x = -10:0.1:10;
for a = 1:3
plot(x, func(x,a ));
hold on;
end
Stephen23
Stephen23 2018년 11월 16일

0 개 추천

You can do this very simply by defining a normal function handle:
>> F = @(x,a) x.^2 + a*x - 6;
>> X = -10:.1:10;
>> plot(X,F(X,1),'r', X,F(X,2),'g', X,F(X,3),'b')
F.png

카테고리

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

제품

질문:

2018년 11월 16일

편집:

2018년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by