I don't know where my code is wrong

This is a code that I wrote. I'm new with matlab so I'm not used to all the grammers and stuffs.
It keeps says 'Invalid expression. When calling a function or i....etc' when I try to run the code. I don't really know how to fix it.
theta=linspace(0,pi/2,256);
k=linspace(-10,10,256);
for i=1:256;
for j=1:256;
syms f(x,y)
f(x,y) = piecewise( (x^2+y^2<=1 & y*tan(theta(i))+k(j)>0),y*tan(theta(i))+k(j),0)
g(x,y)=y*f(x,y);
volume=integral2(f,-1,1,-1,1);
xcm=0;
ycm=integral2(g,-1,1,-1,1);
zxm=volume/pi;
height=2*(ycm+zcm*tan(theta(i));
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
epsilon(length(epsilon)+1)=volume/(pi*height);
distance(length(distance)+1)=(ycm^2+(0.5*height-zcm)^2)^0.5
end
end
plot(epsilon, distance);

1 commentaire

Jan
Jan le 23 Mai 2022
Whenever you mention an error in the corum, post a copy of the complete message.
I've edited your code and ran it to show, where the message occurs.

Connectez-vous pour commenter.

Réponses (2)

Voss
Voss le 23 Mai 2022
This line:
height=2*(ycm+zcm*tan(theta(i));
% 1 2 3 21
is missing a close parenthesis ) somewhere.
Torsten
Torsten le 23 Mai 2022
If
x = r, y = theta and z = z,
the volume of the cylinder below the plane
2x - y + 2z = 2
down to zmin can be calculated as
fun = @(x,y,z) x;
xmin = 0.0;
xmax = 1.0;
ymin = 0.0;
ymax = 2*pi;
zmin = -0.5; % e.g.
zmax = @(x,y) 1 + 0.5*x.*sin(y) - x.*cos(y);
Volume = integral3(fun,xmin,xmax,ymin,ymax,zmin,zmax)

4 commentaires

서준 장
서준 장 le 23 Mai 2022
Thank you so much for your help, It helped a lot.
I wanted to change your code a little bit since I wanted to focus on the area which z>0.
fun = @(x,y,z) x;
xmin = 0.0;
xmax = 1.0;
ymin = 0.0;
ymax = 2*pi;
zmin = -0.5; % e.g.
zmax = @(x,y) piecewise( (1 + 0.5*x.*sin(y) - x.*cos(y))>0, 1 + 0.5*x.*sin(y) - x.*cos(y),0);
Volume = integral3(fun,xmin,xmax,ymin,ymax,zmin,zmax)
Volume =
A lot of errors when I ran this code. Is there an error in 'zmax=@(x,y) pi......' ??
Thank you
Torsten
Torsten le 24 Mai 2022
Modifié(e) : Torsten le 24 Mai 2022
And just changing
zmin = -0.5
to
zmin = 0.0
and
zmax = @(x,y) 1 + 0.5*x.*sin(y) - x.*cos(y);
to
zmax = @(x,y) max(0, 1 + 0.5*x.*sin(y) - x.*cos(y));
is not what you want ?
서준 장
서준 장 le 24 Mai 2022
yes yes this is excacly what I was looking for.
I was kind of confused.
I don't know how to thank you..
Thank you sooo much.. have a nice day, from a student from south korea
Torsten
Torsten le 24 Mai 2022
Greetings back from Germany.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Strategy & Logic dans Centre d'aide et File Exchange

Tags

Commenté :

le 24 Mai 2022

Community Treasure Hunt

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

Start Hunting!

Translated by