# How to plot a complicated function

43 views (last 30 days)
Atom on 15 Apr 2013
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.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Ahmed A. Selman on 15 Apr 2013
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 CommentsShowHide 2 older comments
Ahmed A. Selman on 15 Apr 2013
@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.

Sign in to comment.

### More Answers (1)

Youssef Khmou on 15 Apr 2013
Edited: Youssef Khmou on 15 Apr 2013
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)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!