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.
Related
I have a mixed set of differential and algebraic equations for which I have found the analytical solution in MATLAB. It concerns a point mass moving along a 2D curve constrained by y=x^2.
How would I go about using an ode-solver in MATLAB (or something else if it's easier) to simulate the ball rolling over the curve? The animation I can do myself, I'm more concerned with finding the velocities, xd yd, for each consecutive step. That's where I kind of get lost.
These are the equations of motion which I've derived using Lagrange multipliers. Hence the lambda. The lambda is the reaction force. I can calculate the accelerations, xdd ydd, but I also need the velocities in the state if I want to properly simulate this, I assume.
% Symbolic functions
syms y x xd yd xdd ydd
syms m g lambda
% Parameters
A = [m 0 -2*x; 0 m 1; -2*x 1 0];
X = [xdd ydd lambda].';
b = [0 -m*g -2*xd^2].';
sol = A\b % these are the states stored in X
So if you work out your problem using the lagrancian you would get the following formula seen below (see https://physics.stackexchange.com/questions/47154/ball-rolling-in-a-parabolic-bowl). The k-value comes from y=kx^2 (can be 1 for your example).
So rewrite this in the following form.
Now you just use
ddx= Formula seen above..
x = x + dx *dt
dx = dx + ddx *dt
t = t + dt
y = k*x*x
You make a loop with a sufficient small dt, and update you x-position velocity and acceleration.
Now you need
to specify the following starting values -> x0 dx0 ddx0 and dt.
I hope this helped
Cheers:)
I am working to create a helix in Matlab.
Going by the below code:
t = 0:pi/50:20*pi;
(Can you please explain me this syntax or we have to follow this everytime when creating a helix?)
st = sin(t);
ct = cos(t);
plot3(st,ct,t)
As maximum efficiency in a helix angle is between 40 and 45 degrees, if I want to enter the angle as 42, how is it possible in the code?
It would be very helpful if anyone can share their opinion on this
TIA
What happens in the code is merely an execution of the parametric mathematical description of a helix, which you can read up on wikipedia as
x(t) = cos(t)
y(t) = sin(t)
z(t) = t
The first line of your code generates a vector for values of t from 0 to 20pi in steps of pi/50 (i.e., 1000 steps). Since every 2pi means one full rotation (cos and sin are 2pi-periodic), it coincides to 10 turns of the helix (if you want to change this, let t run up to 2*pi*NumberOfRotations). The other two lines generate corresponding vectors for x and y. plot3 plots a line in 3-D where x and y are passed and as argument for z we pass t since z=t.
To change the slope of the helix, use the more general description given by
x(t) = a*cos(t)
y(t) = a*sin(t)
z(t) = b*t
where a is the radius and b/a is the slope. To get 42° use b = a*atand(42). To make sure the aspect ratio is correct in display, use axis equal; after the plot and maybe axis vis3d; if you want to turn it around.
I'm working on bisection method which I wrote by myself. The following code works fine - it displays correct results(Tested on x^2-25) and it gave correct results. But that's not all what I wanted to realize.I need draw a plot displaying all correct results on it.
As I told - I took expression
x^2-25
, and results are
5 and -5
Now, I need to draw parabola and display results.
My Code
function [] = bisectionWindow()
clc;
f = #(x) x^2-25; % specified function
a=5;
b=6;
e=0.0001;
syms x;
% Main loop
while abs(b-a)>e
c=(b+a)/2;
if sign(f(c)) == sign(f(a))
a=c;
else
b=c;
end
end
disp(['Answer x='])
solve(f(x))
%note: ans displays because of 'sign' operator presence
My Attempt to draw plot
function [] = checkWin(a,b,x)
%draws plot
Limits = [a b];
len = b-a;
for i=1:len
x = X(i);
y=x^2-25;
%y=0.5*x^3-2*x^2+1;
figure(1),clf,hold on
fplot('x^2-25',Limits),grid
plot(x,y,'o')
end
end
UPD: To clear some things up
I understand how to draw a plot. My objective is:
Show the plot
Show results as o's on it
The problem is - this should be unique code (as for example i took parabola, but let's take another function which has only one result) and it should execute things metioned above. So basically this is why i'm working on.
There's 2 options:
Modify existing code - which i dunno how
Rewrite - no ideas how.
I'm stuck for a long time I guess.
Thanks for Advices.
I don't get why you need the loop in your code. The following script works fine and gives the same result as yours:
f = #(x) x^2-25; % specified function
disp(['Answer x='])
disp(solve(f(x)))
You can plot your function by typing ezplot(f):
Typing ezplot(f,[-5,5]) would plot function with x between -5 and 5
If you want to use plot function, you need to rewrite your function as:
f = #(x) x.^2-25
This is because ^2 operation is regarded as multiplication of matrix by itself that is inapplicable to vectors, whereas .^2 is squaring of each matrix element.
Then, you need to create two vectors with x and y data and use plot function:
x = -10:0.1:10;
y = f(x);
plot(x,y), grid on
where grid on provides your plot with gridlines. There are many other plot options that you can use to customize your graph. You can find them here
UPD. Modified code with comments:
f = #(x) x.^2-25; %//specified function
disp(['Answer x='])
q = solve(f); %//-5 and 5 would be in one vector
disp(q) %//display -5 and 5
h = ezplot(f,[-10,10]); %//plot f(x) from -10 to 10
grid on; %//add grid to plot
set(h,'LineWidth',3); %//set line width to 3px
hold on;%//prevent the current graph from being replaced
plot(q,f(q),'ro','MarkerSize',10,'LineWidth',3)
%//plot two O-s at -5 and 5. Also, make O-s 10px in width and 3px thick
hold off;
This yields:
I need a help. I have to generate a curve using MATLAB. The plot is defined by the formula (an analytic expression) :-
where, the meaning of the variables are as follows: R is the distributed resistive function, S is the distributive conductive function, k is the sheet resistance and r(x,y) is the distance between *
(x,y)*, and the perimeter dl with the integration made around all the perimeter of the chip.
A squared foil as shown in the figure with sides (a) 10 arbitrary units long and an unitary unit sheet resistance (k=1 ohm) is used for our consideration. The plot of the function R(x,y) is supposed to come out like this...
I literally have no clue how to plot this function. I could not even get the idea how to define the distance function r(x,y) with respect to dl. On top of that it is complicated further by the closed integral. Please help me. Any help in even simplifying the expression is also welcome. Is there any possible closed form expression for such a square structure ?
Thanks in advance. The link to the paper is here. paper here
Reconstructing the math
The definition of the function R is not particularly clear, but I guess what they mean is:
With dOmega being the boundary of the foil and p a point p = [px,py] on the foil.
Imagine that for a point p on the sheet you are computing R(p) by going around the boundary of the foil (what they call the perimeter), your position being q, and integrating one divided by the distance from you (q) to the point p multiplied by k.
I guess you could analytically compute the integral for this rectangular sheet, but if you just want to plot the function, you could simply approximate the integral by defining a finite number of points on the boundary, evaluating the integrand in those points, then taking the mean and multiplying by the perimeter. [The same way you could approximate integral(f(x), x=0...pi) by pi*(f(0)+f(pi/2)+f(pi))/3]
Alternative representation using coordinates:
If you are only familiar with integrals along the real line in coordinate representation you could write this in the following way, which is frankly quite UGGGLY:
Plotting an approximation
%% Config:
xlen = 10;
ylen = 10;
k = 1;
%% Setting up points on the boundary of the square
x = linspace(0,xlen,50);
y = linspace(0,ylen,50);
perimeter = 2*(xlen+ylen);
boundary = [x(1)*ones(length(y),1), y'
x', y(1)*ones(length(x),1); ...
x(end)*ones(length(y),1), y'; ...
x', y(end)*ones(length(x),1)];
%% Function definition
norm2 = #(X) sqrt(sum(X.^2,2));
R = #(p) 1/(perimeter*mean(1./(k*norm2(bsxfun(#minus,boundary,p)))));
%% Plotting
[X_grid,Y_grid] = ndgrid(x,y);
R_grid = zeros(size(X_grid));
for ii = 1:length(x)
for jj = 1:length(y)
R_grid(ii,jj) = R([x(ii),y(jj)]);
end
end
surf(X_grid, Y_grid, R_grid);
axis vis3d;
This will give you the following plot:
I need to draw an elliptic curve over the finite field F17(in other words, I want to draw some specific dots on the curve), but somehow I don't get it right.
The curve is defined by the equation:
y^2 = x^3 +x + 1 (mod 17)
I tried the way below, but it can't work.
for x = 0:16, plot(x, mod(sqrt(x^3+x+1), 16),'r')', end
Can someone help ?
[Update]
According to Nathan and Bill's suggestions, here is a slightly modified version.
x = 0:18
plot(mod(x,16), mod(sqrt(x.^3+x+1), 16),'ro')
However, I feel the figure is WRONG , e.g.,y is not an integer when x=4 .
You have to test all points that fulfill the equation y^2 = x^3 +x + 1 (mod 17). Since it is a finite field, you cannot simply take the square root on the right side.
This is how I would go about it:
a=0:16 %all points of your finite field
left_side = mod(a.^2,17) %left side of the equation
right_side = mod(a.^3+a+1,17) %right side of the equation
points = [];
%testing if left and right side are the same
%(you could probably do something nicer here)
for i = 1:length(right_side)
I = find(left_side == right_side(i));
for j=1:length(I)
points = [points;a(i),a(I(j))];
end
end
plot(points(:,1),points(:,2),'ro')
set(gca,'XTick',0:1:16)
set(gca,'YTick',0:1:16)
grid on;
Matlab works with vectors natively.
your syntax was close, but needs to be vectorized:
x = 0:16
plot(x, mod(sqrt(x.^3+x+1), 16),'r')
Note the . in x.^3. This tells Matlab to cube each element of x individually, as opposed to raising the vector x to the 3rd power, which doesn't mean anything.
You can use this code if you want to plot on Real numbers:
syms x y;
v=y^2-x^3-x-1;
ezplot(v, [-1,3,-5,5]);
But, for plot in modulo, at first you can write below code;
X=[]; for x=[0:16], z=[x; mod(x^3+x+1,17)]; X=[X, z]; end, X,
Then, you can plot X with a coordinate matrix.