Why does this equation make complex number?

clear;close all;clc; c=2; f=inline('2*c^(-(x-1)^2-(y-1)^2)+1.8*c^(-5*(x+1)^2-3*y^2)-c^(-2*(x-1)^2-3*(y+0.5)^2)','x','y','c') a=-2:.1:2; b=-2:.1:2; [X,Y]=meshgrid(a,b); surf(X,Y,f(X,Y,c))
this equation can't make complex numbers. but if I execute this code, they say that X,Y,Z,C can't be complex number in surf... I think function 'inline' makes something wrong....

Risposte (2)

The ^ operator perform matrix exponentiation. The .^ operator performs element-wise exponentiation.
A = magic(3);
2.^A
2^A
I suspect you want the element-wise version, not the matrix version. As a side note you should also use anonymous functions instead of inline.
c=2;
f=@(x, y) 2*c^(-(x-1)^2-(y-1)^2)+1.8*c^(-5*(x+1)^2-3*y^2)-c^(-2*(x-1)^2-3*(y+0.5)^2)
Now there's no need to pass c into f when you evaluate it.
First off, have a look at the preview before you post a question. This code is unreadable and most people will not bother trying to decipher it. Select your code and then hit the {}Code button.
Now to the cause. Your function is not compatible with matrices (or at least, it does things you don't expect). Your powers should be element-wise and you should use an anonymous function instead of inline.
f=@(x,y,c) 2*c.^(-(x-1).^2-(y-1).^2)+1.8*c.^(-5*(x+1).^2-3*y.^2)-c.^(-2*(x-1).^2-3*(y+0.5).^2);

Categorie

Richiesto:

il 31 Mar 2017

Risposto:

Rik
il 31 Mar 2017

Community Treasure Hunt

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

Start Hunting!

Translated by