How to plot a function ?

조회 수: 3 (최근 30일)
Alexandru Stefan
Alexandru Stefan 2014년 3월 17일
댓글: Alexandru Stefan 2014년 3월 19일
Helli, i'm a newbie in mathlab and i try for hours to make the program below, please would you explained me how to begin or make the program and write the code to see where i make some mistake.
Write a program that recieve as arguments two real numbers a and b with a>0 and b∈(0,1) and who represent graphic in cartesian coordinate function f:[0,2]->R.
{ (a*x)/(x^2+b), x[0,1]
f(x)= {
{ ln(x^2-3*x+3), x(1,2]
with dashed line and black color. The program display an error message if the requirements about arguments are not respected.
Thank You!!

채택된 답변

Carlos
Carlos 2014년 3월 17일
편집: Carlos 2014년 3월 17일
Some ideas here
function y=new(a,b)
if(a<0)
disp('a must be bigger than zero');
end
if(b<0||b>1)
disp('a must be bigger than zero and smaller than 1');
end
x=0:0.1:2;
y=zeros(1,length(x));
for k=1:length(x)
if (x(k)<1)
y(k)=(a*x(k))/(x(k)^2+b);
else
y(k)=log(x(k)^2-3*x(k)+3);
end
end
plot(x,y)
  댓글 수: 3
Carlos
Carlos 2014년 3월 17일
My bad, you are right.
Alexandru Stefan
Alexandru Stefan 2014년 3월 19일
Thank you, both were really helpful.

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

추가 답변 (1개)

Niklas Nylén
Niklas Nylén 2014년 3월 17일
function fx = myFunction(a,b)
x=0:0.1:2; % Create the x vector with step size 0.1
fx = zeros(size(x)); % Initialize the function output fx with zeros
% Make the calculation for when x<=1
index = x <= 1; % Creates a logical vector with 0's and 1's
% Calculate f(x) for the indices of x which fulfill the previous logical statement
fx(index) = (a.*x(index))./(x(index).^2+b);
% Use the second equation when x > 1
index = x > 1;
fx(index) = log(x(index).^2-3.*x(index)+3);
figure;
plot(x,fx)
  댓글 수: 2
Alexandru Stefan
Alexandru Stefan 2014년 3월 17일
Thank You was really helpful, one more question how to make the program show an error if the requirements about arguments are not respected.
Carlos
Carlos 2014년 3월 17일
I have edited my previous code including the imput argument control you requested

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

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by