Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters
显示 更早的评论
This is the code that I have so far. Up until t = linspace(0,2*pi); I do not get any errors. I'm unsure if i have to start a new thread for the second part of this question, but I'm also wondering how would one be able to write a generic code for this type of task.
x = -1 + 2.*rand (1,n)
y = -1 + 2.*rand (1,n)
appr = (sum(x.*sin(1./x)<=1)/n)*4
plot(x(x.*sin(1./x)<=1),y(x.*sin(1./x)<=1),".", ... "Color",[0.3 0.7 0.5], "MarkerSize",10)
hold on
plot(x(x.*sin(1./x0,y(x.*sin(1./x),".", ... "Color", [0.7 0.5 0.3], "MarkerSize",10)
t = linspace(0,2*pi);
plot(cos(t),sin(t),’LineWidth’,3,’Color’,[0.1 0.3 0.5])
回答(1 个)
I had to guess about what you are trying to do.
n = 50;
x = -1 + 2.*rand (1,n)
y = -1 + 2.*rand (1,n)
appr = (sum(x.*sin(1./x)<=1)/n)*4
plot(x(x.*sin(1./x)<=1),y(x.*sin(1./x)<=1),".", ...
"Color",[0.3 0.7 0.5], "MarkerSize",10)
hold on
plot(x(x.*sin(1./x)>1),y(x.*sin(1./x)>1),".", ...
"Color", [0.7 0.5 0.3], "MarkerSize",10)
t = linspace(0,2*pi);
plot(cos(t),sin(t),"LineWidth",3,"Color",[0.1 0.3 0.5])
5 个评论
Tomas
2023-5-2
Walter Roberson
2023-5-2
Your version had
plot(x(x.*sin(1./x)<=1),y(x.*sin(1./x)<=1),".", ... "Color",[0.3 0.7 0.5], "MarkerSize",10)
Everything after the ... on a line is a comment.
Your version had
plot(x(x.*sin(1./x0,y(x.*sin(1./x),".", ... "Color", [0.7 0.5 0.3], "MarkerSize",10)
which has x0 instead of x) and is missing the ) for y(x.*sin(1./x) and is missing the >1 that I presumed are desired to indicate points that do not match with the criteria.
Walter Roberson
2023-5-2
By the way, consider
mask = x.*sin(1./x)<=1;
plot(x(mask), y(mask), stuff)
plot(x(~mask), y(~mask), stuff)
Much simpler and much less error prone.
Tomas
2023-5-2
Image Analyst
2023-5-2
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
