I am trying to make a function which takes a polynomial, a maxvalue of x and a minvalue of x. I want to plot the function between max and min with 1000 points.
this is what i have come up with so far:
function [] = Oppgave2(funksjon, intervallMin, intervallMax)
figure
%a
x = [intervallMin:((abs(intervallMin)+abs(intervallMax))/1000):intervallMax]; %Here i create the 1000 points between min and max.
plot(x, funksjon); %Display the function in a figure
end
I want to be able to input funksjon as x.^2-3.*x+2 for example, but i cant make it work. The solution is probably really simple but i am very new to programming in matlab.
Thanks in advance!

 採用された回答

KSSV
KSSV 2020 年 9 月 10 日

1 投票

LEt a, b be your max and min values
x = linspace(a,b,1000) ;
p = 2*x.^2+3*x+5 ; % polynomial
plot(x,p)

5 件のコメント

Kristoffer Søhagen
Kristoffer Søhagen 2020 年 9 月 10 日
Thats a better way to to get the x values! Thanks! But my main problem is that matlab doesnt like when in call the function like this:
Oppgave2(x.^2-3.*x+4, -5, 5)
I get this error message: Unrecognized function or variable 'x'.
KSSV
KSSV 2020 年 9 月 10 日
You cannot input like this. USe like below:
x = linspace(a,b,1000) ;
f = @(x) 2*x.^2+3*x+5 ;
p = f(x) ; % polynomial
plot(x,p)
Kristoffer Søhagen
Kristoffer Søhagen 2020 年 9 月 10 日
Yeah, that works. But i want to make a function i can call from other scripts to plot a polynomial between a and b. If it is possible.
Example:
function(polynomial, a, b)
function(x.^2+3.*x-4, -5, 5)
KSSV
KSSV 2020 年 9 月 10 日
a =1 ;b = 10 ;
f = @(x) 2*x.^2+3*x+5 ;
p = mypoly(f,a,b) ;
function p = mypoly(f,a,b)
x = linspace(a,b,1000) ;
p = f(x) ; % polynomial
plot(x,p)
end
Kristoffer Søhagen
Kristoffer Søhagen 2020 年 9 月 10 日
Great! Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Polynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by