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
Related
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
This question already has an answer here:
Plotting piecewise function
(1 answer)
Closed 5 years ago.
How can I plot a wave (represented by 1 x N matrix) with different colors in matlab. The range for a specific color can be provided manually.
See the diagram below for the expected output.
Here is a simple option:
x = linspace(-4*pi,4*pi,10000); % some data
y = -sin(x); % some data
N = 4;
py = reshape(y,[],N);
px = reshape(x,[],N);
plot(px,py,'LineWidth',2)
Where y is your vector, and N is the number of pieces you want to distinguish. Note that you have to make sure that y is dividable by N with no remainder.
If you want to set the colors, you can do this with set command:
p = plot(px,py,'LineWidth',2)
cmap = parula(N); % a set of N colors in RGB matrix
set(p,{'color'},mat2cell(cmap,ones(N,1),3))
and you get:
That figure looks like a sine function, so let's just assume that it is for this example. While I don't have MATLAB in front of me right now, what I would probably do is, in an m-file script:
clear all; clc;
functionToPlot = [sin(0 : (pi/2) : (8*pi))]; %This spacing will look very sharp and pointy, so I'd recommend using >>linspace like shown in other answers.
yAxisVector = [-1 : 1 : 1];
for n = 1 : length(functionToPlot)
if rem(functionToPlot(1,n),2) <= pi
plot(functionToPlot(1,n),yAxisVector,'r')
hold on
elseif rem(functionToPlot(1,n),4) <= pi
plot(functionToPlot(1,n),yAxisVector,'g')
hold on
elseif rem(functionToPlot(1,n),6) <= pi
plot(functionToPlot(1,n),yAxisVector,'y')
hold on
elseif rem(functionToPlot(1,n),8) <= pi
plot(functionToPlot(1,n),yAxisVector,'c')
hold on
end
end
This code should give you the function that you pictured in your question. Have you tested a code yet? What code did you test? This link shows an alternative method using RGB values, if you prefer that. Good luck with your project!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Knowing center of the two rectangles and their angle by x axis (horizontal axis), how can one recognize if their intersection is zero or not in Matlab? Any answers containing this information is highly appreciated. Width and length of rectangles are also known
This is a programming problem if you want to solve it numerically. For exact solutions, you could use geometrical equations.
The first problem: defining a rectangle's corners from its width, height and centre:
C1 = [0, 0]; % Centre of rectangle 1 (x,y)
C2 = [1, 1]; % Centre of rectangle 2 (x,y)
W1 = 5; W2 = 3; % Widths of rectangles 1 and 2
H1 = 2; H2 = 3; % Heights of rectangles 1 and 2
% Define the corner points of the rectangles using the above
R1 = [C1(1) + [W1; W1; -W1; -W1]/2, C1(2) + [H1; -H1; -H1; H1]/2];
R2 = [C2(1) + [W2; W2; -W2; -W2]/2, C2(2) + [H2; -H2; -H2; H2]/2];
Next problem is to create many points which represent the edges of the rectangles. You could instead generate many points within the rectangles if you wanted to look at intersecting areas.
n = 1000; % Define some number of points to use
% Use interp1 to interpolate around the rectangles
R1points = interp1(1:5, [R1; R1(1,:)], linspace(1,5,n));
R2points = interp1(1:5, [R2; R2(1,:)], linspace(1,5,n));
Then rotate the rectangles:
a1 = deg2rad(0); a2 = deg2rad(30); % angles of rotation for rectangle 1 and 2 respectively
R1rotated(:,1) = (R1points(:,1)-C1(1))*cos(a1) - (R1points(:,2)-C1(2))*sin(a1) + C1(1);
R1rotated(:,2) = (R1points(:,1)-C1(1))*sin(a1) + (R1points(:,2)-C1(2))*cos(a1) + C1(2);
R2rotated(:,1) = (R2points(:,1)-C2(1))*cos(a2) - (R2points(:,2)-C2(2))*sin(a2) + C2(1);
R2rotated(:,2) = (R2points(:,1)-C2(1))*sin(a2) + (R2points(:,2)-C2(2))*cos(a2) + C2(2);
Finally, check intersection with inpolygon:
in1 = inpolygon(R1rotated(:,1), R1rotated(:,2), R2rotated(:,1), R2rotated(:,2));
in2 = inpolygon(R2rotated(:,1), R2rotated(:,2), R1rotated(:,1), R1rotated(:,2));
If nnz(in1)>0 or nnz(in2)>0 then you have an intersection! Visualise it using scatter:
hold on
scatter(R2rotated(:,1), R2rotated(:,2), '.b')
scatter(R2rotated(in2,1), R2rotated(in2,2), 'xc')
scatter(R1rotated(:,1), R1rotated(:,2), '.r')
scatter(R1rotated(in1,1), R1rotated(in1,2), 'xg')
Result:
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 8 years ago.
Improve this question
This program shows original image, scaled image and cropped image. but this code is how to scaled image. I cannot understand the working and logic of this code. Can anyone here tell me how this code works? thanks in advance.
[row,col] = size(lena);
Scale_Rows = 2*row;
Scale_Cols = 2*col;
Scale_image = zeros(Scale_Rows,Scale_Cols);
for i= 1:row
for j = 1:col
Scale_image(i*2,j*2) = lena(i,j);
end
end
conv_mask = [ 0.25 0.5 0.25; 0.5 1 0.5; 0.25 0.5 0.25];
fin_lena = lena;
figure;
imshow(fin_lena);
title('Scaled Image');
The code here starts by doubling the size of the image in terms of the coordinates:
for i= 1:row
for j = 1:col
Scale_image(i*2,j*2) = lena(i,j);
end
end
So if the original image was
A B
C D
It then becomes:
A X B X
X X X X
C X D X
X X X X
Where X is an empty pixel.
The code then fills in (interpolates) the empty pixels by taking a combination of the pixels around it according to this:
conv_mask = [ 0.25 0.5 0.25;
0.5 1 0.5;
0.25 0.5 0.25];
So that means that you take a weight of 1 for the pixel you are currently on, 0.5 for any pixel thats immediately above or to the side and 0.25 for a nearby diagonal pixel. After doing this for all the pixels the gaps are then filled in. By filling the blank gaps like this you end up with a better looking picture than you get by just doubling the original pixels.
It's just a very simple 2x upsampling routine:
insert zeroes between all samples to get 2x image
apply simple 3x3 low-pass filter to interpolate
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?