How to plot a complicated function

조회 수: 13 (최근 30일)
Atom
Atom 2013년 4월 15일
How to plot a complicated equation
-(r * x / K) + (alpha ^ 2 * (1 - c) ^ 2 * x * y / (1 + alpha * h * (1 - c) * x) ^ 2 * h) + d - d * exp(-2 * tau)=0
where x=d /(alpha*(-theta + d*h)*(-1 + c)); y=r*(K-x)*(-1-alpha*h*x+alpha*h*x*c)/(-1 + c)*K*alpha;
The other parameter values are: r=3.3;K=898;alpha=0.045;d=1.06;h=0.0437;theta=0.215;
I have to plot a tau vs c curve such that tau is varying from 0 to 1.6 & c is varying from 0 to .9. I have used the following code
r=3.3;K=898;alpha=0.045;d=1.06;h=0.0437;theta=0.215;
x=d /(alpha*(-theta + d*h) / (-1 + c));
y=r*(K-x)*(-1-alpha*h*x+alpha*h*x*c)/(-1 + c)*K*alpha;
yourfun=@(tau,c) -(r * x / K) + (alpha ^ 2 * (1 - c) ^ 2 * x * y / (1 + alpha * h * (1 - c) * x) ^ 2 * h) + d - d * exp(-2 * tau)
plot(yourfun)
I am getting a problem here and please help me to solve it, please.

채택된 답변

Ahmed A. Selman
Ahmed A. Selman 2013년 4월 15일
It is the same code you wrote, only add loops over c and tau, and use the function (ezplot) to plot a function handle. And DO NOT use (plot) command to plot functions. So try this
clc
clear
close all
r=3.3;
K=898;
alpha=0.045;
d=1.06;
h=0.0437;
theta=0.215;
for c=0:0.1:0.9
for tau=0:0.1:0.6
x=d /(alpha*(-theta + d*h) / (-1 + c));
y=r*(K-x)*(-1-alpha*h*x+alpha*h*x*c)/(-1 + c)*K*alpha;
yourfun=@(tau,c) -(r * x / K) + (alpha ^ 2 * (1 - c) ^ 2 * x * y / ...
(1 + alpha * h * (1 - c) * x) ^ 2 * h) + d - d * exp(-2 * tau);
ezplot(yourfun)
title( {'\tau is ';num2str(tau);'c is ';num2str(c)} )
pause(0.01)
end
end
  댓글 수: 3
Atom
Atom 2013년 4월 15일
Yes. Its good. But some warning is coming here.
Warning: Function failed to evaluate on array inputs; vectorizing the function may speed up its evaluation and avoid the need to loop over array elements. > In specgraph\private\ezplotfeval at 57 In ezplot>ezimplicit at 253 In ezplot at 153
Ahmed A. Selman
Ahmed A. Selman 2013년 4월 15일
@Youssef, yes it is true.
@Pallav, I've tried vectorizing the function, it generates an error (sizes do not match). You may try specifying similar size of c and tau, and use vectors instead of for..end loops. If the warning bothers you, then add:
warning off % at the beginning of the code
warning on % at the end of the code.
Regards.

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

추가 답변 (1개)

Youssef  Khmou
Youssef Khmou 2013년 4월 15일
편집: Youssef Khmou 2013년 4월 15일
hi,
the vector x depends on the value c too, same as y , so they are also anonymous functions, in this case you have three anonymous functions with hierarchy yourfun[x(c),y(c),tau] , yourfun is built on tau, x and y and x,y are built on c , you have to make three functions, and when you try to plot function, you wont see a result because you are giving only scalar values like yourfun(4,5) that is only a point in 2d space , to get the result you need to supply two vectors c and tau and they must be of same length :
r=3.3;
K=898;
alpha=0.045;
d=1.06;
h=0.0437;
theta=0.215;
x=@(c) d./(alpha.*(-theta+d*h)./(-1+c));
y=@(c) r*(K-x(c)).*(-1-alpha*h*x(c)+alpha*h*x(c).*c)./(-1+c)*K*alpha;
yourfun=@(tau,c) -(r.*x(c)./K)+(alpha.^2.*(1-c).^2.*x(c).*y(c)./(1+alpha.*h.*(1-c).*x(c)).^2*h)+d-d.*exp(-2*tau)
tau=(0:0.1:1);
c=linspace(0,pi,length(tau));
F=yourfun(tau,c);
plot(F), grid on
figure,plot3(tau,c,F), grid on,view(-12,38)

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by