Setting up and plotting functions

조회 수: 5 (최근 30일)
Dan_LDS
Dan_LDS 2020년 1월 31일
댓글: Dan_LDS 2020년 1월 31일
I'm quite new to matlab and I'm trying to grasp how to write functions and plot them as well.
One of my excercises in class asks me to:
"Make a function that plots the equation ax^2+bx+c=0, where a,b,c and x are used as input."
Does that mean I need to define values for a,b,c and x first, in order to plot anything?
Not sure how to best approach this in matlab.
My code so far looks like this:
function F(x,a,b,c);
y = a*x.^2+b*x+c;
plot(x,y)
end
But that doesn't plot anything as it is though..
I was hoping on some input on how to setup a good solution here, or maybe just a simple way by assigning values to the variables.. - Again, my coding skills are completely beginner, so any help is appreciated.
  댓글 수: 2
Adam
Adam 2020년 1월 31일
That should work, for a simple case (i.e. it will plot on whatever the current axis is or create one if none exists), if you call it as e.g.
a = 3;
b = 4;
c = 5;
x = 0:0.1:10;
F( x, a, b, c )
I haven't really put any thought into appropriate values for a, b, c and x there, but that would be the general idea. Don't just hit the big green 'run' button as it is a function that requires you to pass in 4 arguments.
Dan_LDS
Dan_LDS 2020년 1월 31일
Thanks!
function F(x,a,b,c);
a=2
b=3
c=4
x = 0:0.1:10;
y = a*x.^2+b*x+c;
plot(x,y)
end
..But it doesn't plot anything still? I am missing something very basic here i figure, sorry

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

채택된 답변

edward holt
edward holt 2020년 1월 31일
It's rough, but I'm also quite new to Matlab.
You answered your own question - you do need to define x, a, b and c.
x needs to be a vector (a series of values over which your function evaluates).
% xmin and xmax define the range over which your function plots
% xres defines how often a point is plotted (low number - smooth curve,
% high number, rough)
inputs = inputdlg({'x min','x max','x resolution','a','b','c'},'input)')
xmin = str2num(inputs{1})
xmax = str2num(inputs{2})
xres = str2num(inputs{3})
a = str2num(inputs{4})
b = str2num(inputs{5})
c = str2num(inputs{6})
x = xmin:xres:xmax
f(x,a,b,c)
function f(x,a,b,c)
y = a*x.^2+b*x+c;
plot(x,y)
end
  댓글 수: 1
Dan_LDS
Dan_LDS 2020년 1월 31일
Aah! This makes sense, I was missing out some very basic stuff here. Very nice setup btw, that's pretty advanced for me though (yet).
Thanks for the input!

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

추가 답변 (1개)

Jakob B. Nielsen
Jakob B. Nielsen 2020년 1월 31일
It does, indeed, need to be given some inputs. Matlab has no way of knowing what a, b, c and x are.
function F(x,a,b,c);
y = a*x.^2+b*x+c;
plot(x,y)
end
It will work, but you need to tell it inputs. E.g. in the command window try making an x vector for example x=1:0.01:100; and then call your function with some variables for a, b and c that you can pretty much make up as you want. For example
F(x,1,2,3)
That will execute the funtion y=1*x.^2+2*x+3 and then plot x,y.
  댓글 수: 1
Dan_LDS
Dan_LDS 2020년 1월 31일
Yes, I see that now. Pretty basic stuff, but it's still valuable information (for a noob)!
Thanks for the input

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

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by