i want to plot something like this:
3*x*y
with a constraint
x^2+y^2=8
in Matlab, so it should look like this:
http://www.wolframalpha.com/input/?i=maximize+3xy+on+x%C2%B2%2By%C2%B2%3D8
any ideas?
i tryed
ezsurf(#(x,y)3*x.*y)
hold
fimplicit(#(x,y)x.^2+y.^2-8)
but the result is bad, the line plot doesn't follow the surface
i did it!
a=#(x,y)3*x.*y
b=#(x,y)x.^2+y.^2-8
constpl(a,b)
and constpl is my own defined function
function constpl(fun,constraint)
a=fimplicit(constraint);
x=a.XData;
y=a.YData;
z=fun(x,y);
ind=z>=max(z)-0.000001;
x1=x(ind);
y1=y(ind);
z1=z(ind);
ezmesh(fun);
hold on
plot3(x,y,z,'r','LineWidth',2)
scatter3(x1,y1,z1,'rd','LineWidth',3)
hold off
end
Related
Hi I have the following code in Scilab:
>Tc=0;
>Tm=1;
>Tf=1800;
>t=(Tc:1:Tf)';
where t is a vector of 1800 components.
And I am asked to do a piecewise function that satisfies certain conditions,
My first try was to do something on the line of
> function vg=simula_vg(t,Tcg,Tfg,Ag)
> if (t<Tcg | t>Tfg) then
> vg=0;
> else
> vg=Ag*Ag*(1-cos(2*%pi*(t-Tcg)/(Tfg-Tcg)));
>end
>endfunction
But it doesnt work as I am asking it to compare vector and scalars.
Then I tried to write this
>for i=[Tc:1:Tf]
>function vg=simula_vg(t,Tcg,Tfg,Ag)
> vg(t<Tcg)=0
>vg(t>Tfg)=0
>vg((Tcg<=t)&(t<=Tfg))=sin(t(i))
>endfunction
>end
But I doesnt work either and I have run out of ideas, is there anything else I can do? All the variables are well defined
>vm=10;
>Ag=2;
>Tcg=200;
>Tfg=400;
>Ar=2;
>Tcr=1000;
>Tfr=1500;
>As=2;
>fs=0.0008;
>h=20;
>d=0.6;
There are more because there are more functions similar to that one that I have to define and I dont know how. Any suggestions on how to do it?
You can do it like this, where the zero values are defined afterwards:
function vg=simula_vg(t,Tcg,Tfg,Ag)
vg=Ag*Ag*(1-cos(2*%pi*(t-Tcg)/(Tfg-Tcg)));
vg(t<Tcg|t>Tfg)=0;
endfunction
Ag=2;
Tcg=200;
Tfg=400;
Tc=0;
Tm=1;
Tf=1800;
t=Tc:1:Tf;
vg = simula_vg(t,Tcg,Tfg,Ag);
plot(t,vg)
I trying to plot vectors of electric field in scilab. But it always error :
champ: Wrong size for input arguments: Incompatible sizes.
the code:
epsilon0=1e-9/(36*%pi);
q=3e-9;
p=[-1,0,0];
x=-2:0.2:2;
y=-2:0.2:2;
[px,py]=meshgrid(x,y);
for m=1:length(x),
for n=1:length(y),
xp=px(m,n);
yp=py(m,n);
vektorr1x=xp-p(1);
vektorr1y=yp-p(3);
r1=sqrt(vektorr1x^2+vektorr1z^2);
if r1~=0 then
ar1x=vektorr1x/r1;
ar1y=vektorr1y/r1;
E1x=q*ar1x/(4*%pi*epsilon0*r1^2);
E1y=q*ar1y/(4*%pi*epsilon0*r1^2);
else
E1x=0;
E1y=0;
end,
end,
end,
pl=champ(px,py,E1x,E1y,[-2,-1,2,-1]);
You don't have to use loops, the following script does what you want:
epsilon0=1e-9/(36*%pi);
q=3e-9;
p=[-1,0,0];
x=-2:0.2:2;
y=-2:0.2:2;
[px,py]=ndgrid(x,y);
vektorr1x=px-p(1);
vektorr1y=py-p(3);
r1=sqrt(vektorr1x.^2+vektorr1y.^2);
ar1x=vektorr1x./r1;
ar1y=vektorr1y./r1;
E1x=q*ar1x./(4*%pi*epsilon0*r1.^2);
E1y=q*ar1y./(4*%pi*epsilon0*r1.^2);
E1x(r1==0)=0;
E1y(r1==0)=0;
clf
champ(x,y,E1x,E1y,[-2,-1,2,-1]);
To plot fields don't use meshgrid to sample the domain use ngrid instead. Moreover, don't forget to use dot-prefixed operators.
My code is written in a file "plot.m".
If I put the following code in "plot.m", when I call plot("20%"), the Octave GUI will keep opening a new window with a new figure indefinitely.
function X = plot(folderName)
X = 0;
data = ([folderName, "\\summary.txt"]);
NUM_SURVIVED = data(1);
NUM_DATA = size(data)(1)-1;
FINAL_WEALTH = data(2 : NUM_DATA);
%plot FINAL_WEALTH
figure;
plot(1:numel(FINAL_WEALTH), FINAL_WEALTH, '-b', 'LineWidth', 2);
xlabel('x');
ylabel('FINAL_WEALTH');
end
However, if I put the following code in "plot.m" and run it, the program works as intended and will plot data from "summary.txt".
data = ("20%\\summary.txt");
NUM_SURVIVED = data(1);
NUM_DATA = size(data)(1)-1;
FINAL_WEALTH = data(2 : NUM_DATA);
%plot FINAL_WEALTH
figure;
plot(1:numel(FINAL_WEALTH), FINAL_WEALTH, '-b', 'LineWidth', 2);
xlabel('x');
ylabel('FINAL_WEALTH');
Any idea what I am doing wrong in the first section of code? I would like to write it as a function so that I can call it multiple times for different folder names.
When you call plot from the function plot, you get endless recursion. Rename your function and its file.
Just adding to Michael's answer, if you really wanted to name your function as "plot" and override the built-in plot function, but still wanted to be able to call the built-in plot function inside it, this is actually possible to do by using the builtin function to call the built-in version of plot. Your code would then look like this:
function X = plot (folderName)
% same code as before
figure;
builtin ("plot", 1:numel(FINAL_WEALTH), FINAL_WEALTH, '-b', 'LineWidth', 2);
xlabel ('x');
ylabel ('FINAL_WEALTH');
end
Obviously, whether it's a good idea to overload such a core function in the first place is an entirely different discussion topic. (Hint: don't!)
I have a plot that is similar to this:
w=rnorm(9)
z=rnorm(9)
A=as.factor(c(rep(c("A1","A2","A3"),3)))
B=as.factor(c(rep("B1",3),rep("B2",3),rep("B3",3)))
C=as.factor(c("C1","C1","C2","C2","C3","C3","C1","C2","C3"))
xyplot(w~z,type="p",cex=1.4,
panel=function(x, y, ...) {
panel.xyplot(x=z[1], y=w[1],pch=15,col="red",...);
panel.xyplot(x=z[2], y=w[2],pch=15,col="green",...);
panel.xyplot(x=z[3], y=w[3],pch=15,col="blue",...);
panel.xyplot(x=z[4], y=w[4],pch=16,col="red",...);
panel.xyplot(x=z[5], y=w[5],pch=16,col="green",...);
panel.xyplot(x=z[6], y=w[6],pch=16,col="blue",...);
panel.xyplot(x=z[7], y=w[7],pch=17,col="red",...);
panel.xyplot(x=z[8], y=w[8],pch=17,col="green",...);
panel.xyplot(x=z[9], y=w[9],pch=17,col="blue",...);
ltext(x=x, y=y+0.1, labels=C)
})
And now I have been trying a lot without success to get a key like this:
I tried with key function, with legend function, trying to create more than one key in the same graph… I’m lost!!
I know you are doing this in lattice, but ggplot makes this kind of thing pretty easy.
my.data<-data.frame(w,z,A,B,C)
ggplot(my.data,aes(x=w,y=z,colour=A,shape=B,label=C)) +
geom_point(size=3) +
geom_text(hjust=-0.2,vjust=-0.2)
I had a hard time figuring out how to get a guide for the labels. But then I realized that if you have the labels, why do you need a guide?
I am in the process of making a nice graph but I encountered an issue with the extremities of the lines I drew. I do not know why R does not plot the lines all the way until maxxx (10 here). You can see on the graph from the code below what I am talking about (I cannot put picture as my reputation is too low), the extremities of the lines stop before 10:
maxxx<-10
plot(0,type="n",axes=FALSE,xlim=c(0,10),ylim=c(0,10),ylab="",xlab="")
mtext("TROLOLOL",side=3,cex=3)
axis(1,pos=0,at=c(0,10),labels=FALSE)
mtext("R1 ",side=1,line=0,cex=3)
axis(2,pos=0,at=c(0,10),labels=FALSE)
mtext("R2",side=2,line=0,cex=3)
Given_growth_rates<-c(0,0.3,0.5,1,2,5);
K1<-7
g1<-5.5
m<-0.2
R1_isoclines<-numeric(length(Given_growth_rates))
for (i in 1:length(Given_growth_rates)){
R1_isoclines[i]<-((Given_growth_rates[i]+m)*K1)/(g1-Given_growth_rates[i]-m)
}
R1_isoclines
K2<-10
g2<-7
R2_isoclines<-numeric(length(Given_growth_rates))
for (i in 1:length(Given_growth_rates)){
R2_isoclines[i]<-((Given_growth_rates[i]+m)*K2)/(g2-Given_growth_rates[i]-m)
}
R2_isoclines
for (i in 1:length(R1_isoclines)){
lines(rep(R1_isoclines[i],times=length(R2_isoclines[i]:maxxx)), R2_isoclines[i]:maxxx, col=i+1, type="l")
lines(R1_isoclines[i]:maxxx,rep(R2_isoclines[i],times=length(R1_isoclines[i]:maxxx)),col=i+1,type="l")
}
Try to replace the last four lines of your code by:
for (i in 1:length(R1_isoclines)){
lines(rep(R1_isoclines[i],times=2), c(R2_isoclines[i],maxxx), col=i+1, type="l")
lines(c(R1_isoclines[i], maxxx), rep(R2_isoclines[i],times=2),col=i+1,type="l")
}
Maybe that does what you expect.