| Author |
Message |
davyzhu
Joined: 23 May 2004 Posts: 521 Helped: 3 Location: oriental
|
18 Mar 2006 14:15 Matlab How to do z=f(x,y)? |
|
|
|
|
Hi all,
I want to do
X=[-5:0.1:5];
Y=[-5:0.1:5];
Z=log(exp(X)+exp(Y));
I want to get Z as a 101*101 (n*n) vector
But the Matlab give Z as a 1*101 (1*n) vector.
How to get Z 101*101 vector without "for loop"?
Best regards,
Davy
|
|
| Back to top |
|
 |
mimomod
Joined: 25 Jan 2006 Posts: 109 Helped: 15
|
18 Mar 2006 19:35 Matlab How to do z=f(x,y)? |
|
|
|
|
Hi davyzhu,
I am not really clear what you need for Z, but i just guess that you want to have every combination of X and X as parameter for Z. Then try:
X=[-5:0.1];
Y=[-5:0.1];
Z=log(exp(kron(ones(length(X),1),X).')+exp(kron(length(Y,1),Y)));
best
Added after 5 minutes:
sorry....there is some mistake....it should have been...
X=[-5:0.1];
Y=[-5:0.1];
Z=log(exp(kron(ones(length(X),1),X).')+exp(kron(ones(length(X),1),X).') );
Added after 15 minutes:
Sorry moderator, still there is small mistake....it should have been...
X=[-5:0.1:5];
Y=[-5:0.1:5];
Z=log(exp(kron(ones(length(X),1),X).')+exp(kron(ones(length(Y),1),Y)) );
i.e. the second exp() is for Y and also there is no transpose operator.
best
|
|
| Back to top |
|
 |
Google AdSense

|
18 Mar 2006 19:35 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
davyzhu
Joined: 23 May 2004 Posts: 521 Helped: 3 Location: oriental
|
19 Mar 2006 3:15 Matlab How to do z=f(x,y)? |
|
|
|
|
Hi,
I have solved it
X=[-5:0.1:5];
Y=[-5:0.1:5];
[Xnew Ynew] = meshgrid(X,Y);
Z=log(exp(Xnew)+exp(Ynew));
Regards,
Davy
|
|
| Back to top |
|
 |
neils_arm_strong
Joined: 05 Jan 2006 Posts: 274 Helped: 20
|
19 Mar 2006 9:44 Matlab How to do z=f(x,y)? |
|
|
|
|
more simple than above is..
[X,Y]=meshgrid(-5:0.1:5);
Z=log(exp(X)+exp(Y));
this will work fine.
also try
surf(X,Y,Z); in the above code at the end.
|
|
| Back to top |
|
 |