I need help to solve my matlab problem in plot surface [closed] - matlab

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Please can anyone help me to solve my Matlab problem. I will share the question and it need to be solved in Matlab. Thanks in advance for your help.

Using meshgrid() to Plot Surface
This method uses two vectors to set the axes. After creating the axes the grid over coordinates to plot over (domain) is created using the meshgrid(). The meshgrid() function will take two vectors that describe the axes. After creating this grid of coordinates the function k can be evaluated for all the points on the grid-pair.
For 6 ≤ n ≤ 12 (ten values) and 0.001 ≤ p ≤ 0.009 (five values):
Here you can see the top row of images showing the vectors used to create the axes. The second row of images shows the grid formation created using meshgrid(). You can see by taking one cell/value from n and one cell value from p a coordinate pair is formed → (n,p). For example, one coordinate point can be (n,p) → (6,0.0030).
For 6 ≤ n ≤ 12 (ten values) and 0.01 ≤ p ≤ 0.1 (ten values):
Number_Of_P_Ticks = 5;
P_Axis = linspace(0.001,0.009,Number_Of_P_Ticks);
Number_Of_N_Ticks = 10;
N_Axis = linspace(6,12,Number_Of_N_Ticks);
[p,n] = meshgrid(P_Axis,N_Axis);
k = -p.*n + sqrt((p.*n).^2 + (2.*p.*n));
subplot(1,2,1); surf(p,n,k,'FaceAlpha',0.9);
title("10 Values of p and 5 Values of n");
xlabel("p"); ylabel("n");
Number_Of_P_Ticks = 10;
P_Axis = linspace(0.01,0.1,Number_Of_P_Ticks);
[p,n] = meshgrid(P_Axis,N_Axis);
k = -p.*n + sqrt((p.*n).^2 + (2.*p.*n));
subplot(1,2,2); surf(p,n,k,'FaceAlpha',0.9);
title("10 Values of p and 10 Values of n");
xlabel("p"); ylabel("n");
Ran using MATLAB R2019b

Related

Assign a value to each element of a matrix inside a given region in Matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an y*x matrix in matlab, and I want to assign a value to any point of the region of the matrix between 4 points, like A,B,C,D the image of which I know the coordinates, if the point were aligned this wouldn't be a problem, but they aren't.
Is there a function to do this ?
Edit
I have an an input matrix of numbers between 0 and 1.
The size of the matrix is 720*1280
The region is defined like this
x = [3 10 27 20 3];
y = [10 40 31 1 10];
It doesn't matter too much if the pixels are excluded or included along the edge of the rectangle, but is better if they are included.
The output matrix should be equal to the input matrix but with the values of the points inside the region of interest replaced by some other value, for example 2.
The solution I was searching for is very similar to the one that m7913d gave me.
You can use poly2mask to convert the polygon to a mask and then change the desired elements in the matrix using logical indexing as follows:
% Create a sample matrix
matrix = rand(50, 50);
% Define your region as a polygon
x = [3 10 27 20 3];
y = [10 40 31 1 10];
% Convert the polygon to a mask
mask = poly2mask(x,y,size(matrix, 1),size(matrix, 2));
% Change the elements in matrix which are inside the polygon
matrix(mask) = 123;
% Display the result
figure
imshow(matrix, 'InitialMagnification', 'fit')
hold on
plot(x, y, 'r')

Linear interpolation inside a 3D triangle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have data at the nodes of a 3D triangle, and I need to interpolate to get data inside the triangle.
here is what i tried to do:
x=[0,1,0];
y=[1,0,1];
z=[0,2,-1];
[X,Y,Z]=meshgrid(x,y,z);
v=[2,5,-1];
xs=linspace(0,1,.1);
ys=linspace(0,1,.1);
zs=linspace(-1,2,.1);
Vs = interp3(X,Y,Z,v,xs,ys,zs,'linear');
i get an error: The number of input coordinate arrays does not equal the number of dimensions (NDIMS) of these arrays.
what is wrong?
Let Xcontain the x-coordinates of your nodes, Y the y-coordinates, Z the z-coordinates of your nodes. Store the value/data at your nodes in V. Now you can specify where you want to interpolate the data by saving the x,y and z-coordinates of these points in Xs,Ys and Zs. The value of your data at these points is:
Vs = interp3(X,Y,Z,V,Xs,Ys,Zs,'linear');
You can take a look at the Matlab documentation.
Edit: As you added your code: The error seems to be that the dimension of your V is wrong. If you take a look at the example Matlab Docu -> interp3 -> Evaluate Outside the Domain of X, Y, and Z you will see, that Vhas to have the dimension as X, Yand Zcombined. From the documentation: size(V) = [length(Y) length(X) length(Z)] for vectors X ,Yand Z.
Here is an example:
X = linspace(-1,2,5);
Y = linspace(-1,7,23);
Z = linspace(3,9,23);
V = rand(23,5,23);
xq = linspace(0,1,34);
yq = linspace(0,2,34);
zq = linspace(4,5,34);
vq = interp3(X,Y,Z,V,xq,yq,zq,'linear',-1);

How to solve an equation with two variables in MATLAB [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
For example, I want to solve equation |x|+|y|=1 numerically, I would to get a rectangle. If I plot it out, it should be something like
It's a simple example. What I really want to solve is a equation like sin(x)sin(2y)+sin(y)sin(2x)=0.
It really pays to do a little math before jumping to fancy graphs or numerical solvers.
Suppose you have
sin(x)·sin(2y) + sin(y)·sin(2x) = 0
From any standard list of trig identities you find that this can be re-written as
sin(x)·sin(y) · (cos(x) + cos(y)) = 0
So your equation holds when
sin(x)·sin(y) = 0 or
cos(x) + cos(y) = 0
in other words,
x = 0 ± k·π, or
y = 0 ± k·π, or
x = -y ± 2k·π
with k ∈ ℤ0.
Graphically, this would be a set of vertical lines (x = 0 ± k·π), a set of horizontal lines (y = 0 ± k·π) and a set of lines at ±45° (y = -x ± 2k·π).
Not a line of code needed.
With your example equation, Rody's answer provides an analytic solution.
In general, however, given an equation f(x,y)=0 it may not be possible to find an analytic solution. In that case I suggest:
Define a target x, y area and generate random samples within it.
Compute z = abs(f(x,y)) at the sample points.
Sort the values of z and define the approximate solution set as a given proportion of the lowest values.
f = #(x,y) sin(x).*sin(2*y)+sin(y).*sin(2*x); %// your example function
N = 1e7; %// number of samples
proportion = 1e-2; %// choose to achieve thin lines in solution set
xmin = -10; xmax = 10; %// x limits of target area
ymin = -10; ymax = 10; %// y limits of target area
x = xmin+(xmax-xmin)*rand(1,N);
y = ymin+(ymax-ymin)*rand(1,N);
z = abs(f(x,y));
[zsort isort] = sort(z);
n = round(N*proportion);
plot(x(isort(1:n)),y(isort(1:n)),'b.','markersize',.1)
axis square

coloring specific point according to a given parameter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to matlab coding and I know it's simple that's why I'm trying to plot 2D datas read from a file,but I'm stuck,with my following code I'm reading the x y coordinates and trying to plot specific point with specific indexing according to given criteria ( which is p in my case I won't go too much into details), all I want to know is how can I modify the code so that I can give the correct index to the point that I want to color(i.e when the condition is satisfied plot this specific points with blue or watever),here is my code :
M=load('data1.XYZ');
x=M(:,1); %all x coordinates
y=M(:,2); %all y coordinates
xA=M(1:400,1); % x of particles A
yA=M(1:400,2); % y of particles A
xB=M(401:800,1); % x of particles B
yB=M(401:800,2); % y of particles B
Pos1=[x y]; % read in the x y coordinates
[num1,junk1] = size(Pos1);
PosA=[xA yA]; % read in the x y A coordinates
PosB=[xB yB]; % read in the x y B coordinates
[numA,junkA] = size(PosA);
[numB,junkB] = size(PosB); %no of all B particles
fprintf('Determining Distances between particles...\n');
r = zeros(numA,1);
psil_avg=0.0+0.0i;
psir_avg=0.0+0.0i;
for m=1:numA
for n=1:numA
cnt_l=0;
psi_l=0.0+0.0i;
if(m~=n)
r(m,n)=norm(PosA(m,:)-PosA(n,:));
if(r(m,n)< 1.44)
v1=PosA(m,:)-PosA(n,:);
u=[0 1];
dot=v1(:,1).*u(:,1)+v1(:,2).*u(:,2);
N=norm(v1);
cosinus=dot/N;
theta=acos(cosinus);
cnt_l=cnt_l+1;
psi_l=psi_l+(cos(theta)+6.0i*sin(theta));
psil_avg=psi_l/cnt_l;
for k=1:numA
cnt_r=0;
psi_r=0.0+0.0i;
if(m~k)
r(m,k)=norm(PosA(m,:)-PosA(k,:));
if(r(m,k)< 1.44)
v2=PosA(m,:)-PosA(k,:);
u2=[0 1];
dot2=v2(:,1).*u2(:,1)+v2(:,2).*u2(:,2);
N2=norm(v2);
cosinus2=dot2/N2;
theta2=acos(cosinus);
cnt_r=cnt_r+1;
psi_r=psi_r+(cos(theta2)+6.0i*sin(theta2));
psir_avg=psi_r/cnt_r;
p=sqrt(psi_r*psi_l);
if p > 0.94
% fprintf('bond order parameter is %f\n',p);
plot(xA(n),yA(n),'ro','Markersize',6);
hold on;
else
plot(xA(n),yA(n),'go','Markersize',8);
end
end
end
end
end
end
end
end
if anyone can help I'd be thankful
Use scatter and the following properties:
'MarkerEdgeColor' — Marker edge color
[0 0 1] (blue) (default) | 'auto' | 'none' | three-element RGB vector | string
'MarkerFaceColor' — Marker face color
'none' (default) | 'auto' | three-element RGB vector | string
Your code is a bit hard to read so I will adress this quiestion in general.
If you have groups of coordinates you would like to plot with different colors say X1,Y1 and X2,Y2 you may do the following
figure
plot(X1,Y1,'r*')
hold on
plot(X2,Y2,'b*')
hold off
This will color the first group in red with a dot and the second with blue and a dot.
The hold on, hold off is made to plot more then one plot on a single axis without clearing the previous one.
As suggested in the comments - you should probably try to avoid looping in matlab.
I could not understand your question clearly. Do you mean like this ? For example if I have two plots
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
figure
plot(x,y1,x,y2)
Do you want different colours say red for 10,blue for 20, green for 30 degrees for both curves ? Is this what you want ? Please specify properly.

Matlab, hamiltonian system, plotting and energy calculation with Euler (backwards, forwards, mid point and semi-implicit) [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
This is the 2 body kepler problem.
The Hamiltonian is
This is supposed to be an "ellipse". How in the name of all that is holy can it be an ellipse when the initial values only have velocity components on one axis, and starting coordinates are on the same axis?
How do you plot the numerical solution using backwards euler?
I convert the two second-order equations into four first-order equations and solve using ode23 (not backwards Euler, but a different numerical method).
odefun = #(t, y) [y(3);
y(4);
-y(1) / (y(1)^2 + y(2)^2)^(3/2);
-y(2) / (y(1)^2 + y(2)^2)^(3/2)]
p0 = [1 0]; % initial position
v0 = [0 1]; % initial velocity
[t y] = ode23(odefun, [0 20], [p0 v0])
comet(y(:,1), y(:,2))
You can see now that the path traced is an ellipse (actually a circle for my initial velocity). You can play with the inital conditions (v0 = [0 1.1] gives you an ellipse).
Maybe i missunderstood what the system actually means, but what i see is a system with two bodies only under the influence of each others gravity. Starting position for body 1 is not a vector, but only a scalar, so it can only be located on the X axis. In this case it's coordinates are x = 1-a (0 < a < 1). Body 2 has it's starting position on x=0. Now, there is no force vectors pointing them away from the x axis, since the gravity gradient always is parallell to that same axis. The starting velocity is of course also a scalar, so there is no initial veolcity pointing them anywhere but on a parallell trajectory to the x axis.
When you say "this forms an ellipse", maybe i'm looking at the wrong coordinate system. I did the forward Euler plots as follows:
clear all, close all, clc
stl= 0.0005; % steplength
nos = 50000; % number of steps
q1 = zeros(1,nos);
q2 = zeros(1,nos);
q1v = zeros(1,nos);
q2v = zeros(1,nos);
q1a = zeros(1,nos);
q2a = zeros(1,nos);
% Starting positions
a=0.5;
q1(1) = 1-a;
q2(1) = 0;
q1v(1) = 0;
q2v(1) = sqrt((1+a)/(1-a));
% P is inertia, and has the same vector components as the body velocity
for i=1:nos-1
[q1a(i),q2a(i)] = u1FUNC(q1(i),q2(i));
q1v(i+1) = q1v(i) + q1a(i).*stl;
q2v(i+1) = q2v(i) + q2a(i).*stl;
q1(i+1) = q1(i) + q1v(i+1)*stl;
q2(i+1) = q2(i) + q2v(i+1)*stl;
end
plot(q1)
hold on
plot(q2,'r')
u1FUNC
function [a,b] = func(c,d)
% [a,b] = [q1'',q2'']
% [c,d] = [q1,q2]
a = -c/(((c^2)+(d^2))^(3/2));
b = -d/(((c^2)+(d^2))^(3/2));
end
Now, this code gives a plot where q1 and q2 are plotted with q1 and q2 on the y axis and time on the x axis, which seems logical to me, if q1 and q2 decribes the distance for the body in question from the origo in a non-moving reference system. But how do you make it look like an ellipse?