Drawing a plot and calculating area in matlab - matlab

I need to draw the curve of x^3+y^3-3*axy=0 and calculate its area. I'm new to matlab so I don't know much. Any help would be appreciated.
Here's what I came up with already`
function makeres(a)
syms x y;
k=y.^3+x.^3-3.*x.*a.*y==0;
ezplot(k)
fun=#(x)(k);
integral(integral(k,0,5),0,5)
But I get an error on integral.

Try this:
syms x y;
k=y.^3+x.^3-3.*x.*a.*y==0;
ezplot(k)
fun=#(x,y)y.^3+x.^3-3.*x.*a.*y;
g = integral2(fun,0,5,0,5);
Your erros where in the last 2 lines:
a - you were defining k (which is an equation) as the function to be integrated
b - then you integrated k, not fun
c - it is better use integral2 for double integral
I'm don't know what a should be, it works for me when a is a number, an the code as a single script (not a defined function).

Related

Two MATLAB questions about syms

I use matlab syms to define a function
f(x,y,z) = ||y-zx||^2
where x and y are vectors of R^5, z is a scalar in R as follows
syms x y [5,1] matrix;
syms z;
f = (y-z*x).'*(y-z*x)
Now, I have two questions:
How to expand the expression of f using MATLAB?
How to define the function t-> d f(x+tw,y,z)/d t via an output of diff?
Thanks a lot for any comments and suggestions.
What I tried are summarized below:
How to expand the expression of f using MATLAB?
It seems that
expand(f)
does not work! and the other functions such as simplify, collect and factor work neither. So I want to know how to expand and simplify this expression in the correct way?
How to define the function t-> d f(x+tw,y,z)/d t via an output of diff?
I have tried to define the function phi(t) = f(x+tw,y,z) first as
syms x y w [5,1] matrix;
syms z t;
f = #(x,y,z) (y-z*x).'*(y-z*x);
phi = #(t) f(x+t*w,y,z);
Then I compute the derivative using diff:
diff(phi(t),t);
But I don't know how to make the resulting expression as a function of t (so that I can evaluate the expression of phi'(1))? For the moment, I just copy the expression of phi'(t) computed by diff and define it manually. Hoping to get a better way to do so. Thanks a lot for any comments and suggestions.

Matlab cart2pol function

Does the Matlab's cart2pol() function calculate the value of theta differently? Here's an example i worked out when converting to polar coordinates using cart2pol.
First, i implement it with cart2pol.
N = 8;
x = 1:N; y = x;
[X,Y] = meshgrid(x,y);
[theta,rho] = cart2pol(X-floor(N/2),-(Y-floor(N/2)))
which gives
Now, if i use the equation which is:
Note in Matlab, -1 and +1 is not required
theta = atan2((N-2.*Y),(2.*X-N))
i get:
Why is it that the value 3.1416 is negative for the cart2pol function and positive based on the equation?
As beaker said, the angle is the same. But regarding your comment: You can always use edit <functionname> to see how the Matlab function is implemented. This usually works for .m files, but not for P and MEX files, since these are binary and therefore can't be edited directly. So
edit cart2pol
in this case and you'll see that all cart2poldoes, is just atan2(y,x) to get theta. So this means that the different results are just due to the fact that you call the function using different inputs than the inputs you use in your "formula".
atan2(-(Y-floor(N/2)),X-floor(N/2))
gives exactly the same result as your call of cart2pol.

Evaluating a complex integral in MATLAB

Hello I'm trying to integrate the following function in MATLAB
And this is my attempt at evaluating it at a given (x,y)
fun = #(t,x,y) exp(1i.*(t.^4+x.*t.^2+y.*t));
P = #(x,y) integral(#(t)fun(t,x,y),-Inf,Inf);
P(1,1)
According to WolframAlpha the answer is 1.20759 + 0.601534 i for P(1,1)
but MATLAB returns -6.459688464052636e+07 - 8.821747942103466e+07i
I am wondering how to enter an integral like this correctly.
I have now also tried evaluating this symbolically and using a taylor series to approximate but still no luck.
syms x y t
x=1
y=1
f = exp(1i*(t^4+x*t^2+y*t));
fApprox = taylor(f, t, 'ExpansionPoint', 0, 'Order', 10)
sol=int(fApprox,t,[-inf inf])
Any additional suggestions
Many thanks in advance.

matlab - 2 second degree ODE plot with runge kutta - numerical methods

The following equation is given.
I know that i need to break the 2 second order ODE's into 4 first order ODE's. This i what i've got. Ive first introduced the new variable u and in the bottom of the picture i've written my matlab function that i use with ODE45.
Now the problem is that im supposed to get a parabola shaped figure(blue line) but that is not what I am getting.
I've gone through my code a thousand times with no results. Can detect any errors in my function?
main program
global g H R alfa
alfa=pi/2;
g = 20.0;
R = 1;
H=2.3;
k = 0:0.01:2;
[T,Y] = ode45(#fspace,k,[H 0 0 0]);
plot(T,Y(:,1))
hold on
fi= 0:2*pi/60:2*pi;
xx =R*cos(fi);
yy =R*sin(fi);
plot(xx,yy)
function f
function f = fspace(x,u)
global g R H alfa G
G=(g*R.^2)./((R+H).^2);
f = [u(2) G*cos(alfa)-g*((R.^2)/u(1).^2)+u(1)*u(4)^2 u(4) (G*sin(alfa)-2*u(2)*u(4))/u(1)];
I think your problem lies with those lines:
fi= 0:2*pi/60:2*pi;
xx =R*cos(fi);
yy =R*sin(fi);
plot(xx,yy)
You are plotting a circle of radius R. phi is part of the solution from the ode solver, so you should have instead:
plot(R*cos(Y(:,3)),R*sin(Y(:,3)))
but that will always give you a circle of radius R, never a parabola. Or is the parabola meant to refer to plot(T,Y(:,1)).
The equations and the code appear to be correct as far as I can see. Replacing your definition of phi by Y(:,3) gives essentially the same plot, except that the resolution is less. As I said, you'll always get a circle by plotting yy vs xx. You need to clarify what the parabola should refer to.

parametric integration and numeric plotting of a 2D function

I am going to devise a 2D function as a probability density function, is which a function of two variables, i.e. f = f(x,n). Then, as the target is plotting the probability variation, the integration in related to parameter x should be taken into account. The t parameter is the variable planned to be the upper bound of the integration. It is suffice to say that n is the other tuning factor. Finally, with due attention to the considered meshgrid, the probability surface is supposed to be drawn.
My option for the integration process is the symbolic int function. But there is an error: Error using mupadmex
Here is my code:
clear;
syms x;
syms n;
syms t;
sigma = 1;
mu = 0;
[t,n] = meshgrid(0:0.01:20, 1:1:100);
f = (n./(2*sigma*sqrt(pi))).*exp(-((n.*x)./(2.*sigma)).^2);
ff = int(f, x, -inf, t);
mesh(n,t,ff);
And the error trace:
Error using mupadmex
Error in MuPAD command: The argument is invalid. [Dom::Interval::new]
Error in sym/int (line 153)
rSym = mupadmex('symobj::intdef',f.s,x.s,a.s,b.s,options);
Error in field (line 14)
ff = int(f, x, -inf, t);
Would you please helping me to overcome this tie?!
PS. I know that there are some ideas to do this stuff more numerically by integral function, but I am prone to handle this case by int function, if it is possible. Because this code should be used as a service by the other snippets and the generated ff parameter is completely deserving, however it won't be a closed form function.
Thanks in advance.
I have changed several details in your code, Ill try to explain all of them.
No need of defining symbolic variables that are not going to be symbolic.
code:
clear;
syms x;
sigma = 1;
mu = 0; % This is never used!
You want to integrate a function f(n,x) dx for different n. If you create a meshgrid of n (instead of a vector), you will have tons of repeated f(ni,x) that you are not going to use.
Just do:
t=(0:1:20);
n=(1:10:100);
% are you sure you dont want ((n.*x)-mu) here?
f = (n./(2*sigma*sqrt(pi))).*exp(-((n.*x)./(2.*sigma)).^2);
int does not accept a mesh of symbolic functions! (or I haven't managed to make it work...).
So put a couple of for's there!
for ii=1:length(n)
for jj=1:length(t)
ff(ii,jj) = int(f(ii), x, -inf, t(jj));
end
end
Now we DO want a meshgrid for the plot!
Like this:
[t,n] = meshgrid(t, n);
And you want to plot the numerical value, so use double() to convert from symbolic to numerical:
plot!:
mesh(n,t,double(ff));
Result (with low amount of points due to the obvious computational effort needed)