finding streamline and plotting it on a graph - matlab

I am trying to plot the streamlines for the following flow:
.
We are plotting our stagnation points with a sparatrix line but somehow the streamline just goes over it...
We cannot use the stream function. Any idea what is wrong?
Somehow the plot is not plotting correctly...here is my matlab code:
%% constant parameters
a=1;
U=100;
T=3*pi*a*U;
%% plot the stagnation points
syms z
k=100./z^2 - 1i*3./(2*z) - 100==0; %diff(w)==0
stagpoint=solve(k,z); %get the points
plot(stagpoint,'MarkerFaceColor','g');
%% plot the the sparatrix line
[x,y]=meshgrid([-7:0.1:7],[-2:0.1:2]);
z=x+1i*y;
w1=-100*(z+1./z)-1i*log(z).*3*pi*100./(2*pi);
psi=imag(w1);
limit=sqrt(x.^2+y.^2);
x((limit<0.99))=NaN;
y((limit<0.99))=NaN;
contour(x,y,psi,'k');
xlabel('Re');
ylabel('Im');
title('streamline for question4(a)');
axis([-5 5 -5 5])
daspect([1 1 1]);
hold on
%% plot streamline
RK4_4A(z);
%% plot the surface of the cylinder
angle=[0:0.1:360];
x=cosd(angle); %x component of the cylinder
y=sind(angle); %y component of the cylinder
plot(x,y);
hold on
fill(x,y,'r');
Here is the function we use for the 4th Order Runge-Kutta Method:
function RK4_4A(zn)
h=0.001;
x=5;
y=[-5:0.5:5];
zn=x+1i*y;
for j=1:length(y)
z(1)=zn(j);
t=0:h:2;
for i=1:length(t)-1
k1 = ffunc(z(i));
k2 = ffunc(z(i)+h/2*k1);
k3 = ffunc(z(i)+h/2*k2);
k4 = ffunc(z(i)+h*k3);
z(i+1) = z(i) + h*(1/6*k1+1/3*k2...
+1/3*k3+1/6*k4);
end
plot(real(z),imag(z),'b');
hold on
end
function s=ffunc(z)
s = 100./z^2 - 1i*3./(2*z) - 100;
s=conj(s);
end
end

Related

plotting a bullet-nose curves

I would like to plot this function of Two Variables you can find it here
$$z^2=t(t-i) \Longleftrightarrow x^2+y^2=4x^2y^2 \Longleftrightarrow y=\dfrac{\pm x}{\sqrt{4x^2-1}} \mbox{ with } |x|>\frac{1}{2}$$
would someone show me step by step how to plot this in matlab
is there any script or toolbox in http://www.mathworks.com/matlabcentral/fileexchange
which make plot of that kind of curves quickly
this is by geogebra
This is by wolframe
You can use symbolic variables with ezplot.
syms x y % makes symbolic variables
h1 = ezplot('-4*x^2*y^2+x^2+y^2'); % plots the equation
axis equal
set(h1, 'Color', 'k');
Or you can define a function,
f = #(x,y) -4.*x.^2.*y.^2+x.^2+y.^2;
h1 = ezplot(f);
set(h1, 'Color', 'k');
It won't be easy to have the axis in the middle, I hope it's not necessary to have that.
Edit
You can download oaxes here
syms x y
h1 = ezplot('-4*x^2*y^2+x^2+y^2');
axis equal
set(h1, 'Color', 'm');
oaxes('TickLength',[3 3],'Arrow','off','AxisLabelLocation','side',...
'LineWidth',1)
Edit
For 3D plot try this,
% First line provides a grid of X and Y varying over -5 to 5 with .5 as step-size
[X,Y] = meshgrid(-5:.5:5);
% instead of "=0", Z takes the values of the equation
Z = -4 .* X.^2 .* Y.^2 + X.^2 + Y.^2;
surf(X,Y,Z) % makes a 3D plot of X,Y,Z
You can also try contourf(X,Y,Z) for 2D plot.

Animating plot with matlab / octave

I'm trying to animate a plot for this equation see below I'm trying to animate it for b when 300>= b <= 486
clear all, clc,clf,tic
m=3.73;
a=480;
b=486;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
plot(x,y)
I'm using octave 3.8.1 which is a clone of matlab
Put your plotting code in a for loop with b as the iterating variable, then place a pause for a small amount of time. After, plot your graph, then use drawnow to refresh the plot. In other words, try this code. I've placed %// Change comments in your code where I have introduced new lines:
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
figure;
for b = 300 : 486 %// Change
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
pause(0.1); %// Change
plot(x,y);
title(['b = ' num2str(b)]); %// Change
drawnow; %// Change
end
As a bonus, I've put what the current value of b is at each drawing of the plot. BTW, I don't know why normalize_y is in your code when you aren't using it. Do you mean to plot normalize_y instead of y? Just an after thought. Anyway, try that out and see how it looks. Good luck!
Another solution would be to use the handle of a plot and then only update the 'YData'-property of a plot. That is especially useful for more complex plots where there are more than 1 line but you only want to change one line. Also Axis-labels are not overwritten then, which generally prevents alot of overhead.
In Matlabcode it could look like this:
% // Parameter and x-range
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
% // function to compute y for given x and parameter b
f = #(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
% // first plot out of loop (to get plot handle)
figure;
b = 300;
y = f(x, b);
handle = plot(x, y);
xlabel('x') % // only set once
ylabel('y=f(x,b)') % // only set once
title(['b = ' num2str(b)]);
pause(0.1);
% // animate for b = 301 to 86
for b = 301:486 %// Change
set(handle, 'YData', f(x, b)) % set new y-data in plot handle
pause(0.1); %// update plot
title(['b = ' num2str(b)]); %// update title
end
This will work with octave 3.8.1
% // Parameter and x-range
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
% // function to compute y for given x and parameter b
f = #(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
% // first plot out of loop (to get plot handle)
figure;
b = 300;
y = f(x, b);
handle = plot(x, y);
xlabel('x') % // only set once
ylabel('y=f(x,b)') % // only set once
title(['b = ' num2str(b)]);
pause(0.1);
% // animate for b = 301 to 86
for b = 301:486 %// Change
%set(handle, 'YData', f(x, b)) % set new y-data in plot handle
%To work with octave 3.8.1 use the line below
set(handle, 'YData', real (f(x, b)))
pause(0.1); %// update plot
title(['b = ' num2str(b)]); %// update title
end

create subplot with loop

i have following question and please help me to solve following task:
i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this
subplot(2,2,1)
subplot(2,2,2)
subplot(2,2,3)
subplot(2,2,4)
but can i do linear form?like 1:100?or something like this ,more generally like this
n=100;
for i=1:n
subplot(1,n,i)
?
thanks very much
EDITED CODE
function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
[Pxx,f]=pburg(y,i,ndft,fs);
subplot(ndft,ndft,i);
plot(f,Pxx);
title(['order',num2str(i),'i']);
order=i;
end
end
picture :
i can't understand what happens
1-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
figure,
for k=1:4
subplot(4,1,k)
plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
xlim([0 40])
end
Output
2-D Demo
Code
%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);
%%// Plot
colors=['r' 'g' ; 'y' 'k'];
figure,
for k1=1:2
for k2=1:2
subplot(2,2,(k1-1)*2+k2)
plot(t,y1,colors(k1,k2));
end
end
Output
Hopefully these demos would guide to you something meaningful for your case.
Yes, it is:
n=5;
for i=1:n
subplot(1,n,i)
end
gives
for pat=1: N % main loop
% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns
% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number
% Find the subplot number
sub_fig=mod(pat,s1*s2) % subplot number
% correct for corners
if(sub_fig==0)
sub_fig=s1*s2;
fig_num=fig_num-1;
end
% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;
plot(1,1) % plot something
end % of main loop

Plotting of a expotential curve between an interval in MATLAB

I would like to plot an expotential curve between an interval based on a different multipler for each interval.
I have tried this:
%Plotting of h curve
function PlotQ(Time,h)
for i=2:size(Time,1)
for t=Time(i-1):0.1:Time(i)
plot([Time(i-1), Time(i)],exp(-h(i)*t))
hold on;
end
end
ymax = max();
xlim([1 max(Time)]);
ylim([-0.5 ymax+0.5]);
xlabel('Time')
ylabel('Rate')
end
The curve comes out like this:
Not sure what I am doing wrong.. Need some guidance..
New Edit:
T =[0;0.569444444444444;1.06666666666667;2.08611111111111;3.09722222222222;4.11111111111111;5.12500000000000;7.16111111111111;10.2000000000000;20.3444444444444;30.4944444444444];
%Plotting of h and Q
h = [0;0.0187;0.0194;0.0198;0.0215;0.0225;0.0241;0.0316;0.0379;0.0437;0.0452];
PlotQ(Time,h)
If I understood correctly, You are looking for something like this.
%Plotting of h curve
function PlotQ(Time,h)
for i=2:size(Time,1)
tVector=Time(i-1):0.1:Time(i);
sizetVector=length(tVector);
for t=2:sizetVector
plot([tVector(t-1), tVector(t)],[exp(-h(i)*tVector(t-1)),exp(-h(i)*tVector(t))]);
hold on
end
end
ymax = max();
xlim([1 max(Time)]);
ylim([-0.5 ymax+0.5]);
xlabel('Time')
ylabel('Rate')
end
I think, this is what you want:
function PlotQ(Time,h)
% Parameters
res = 0.1;
n = numel(h);
% Pre-allocate
y = cell(1,n);
t = cell(1,n);
% Calculate
for ii=2:n
t{ii} = Time(ii-1):res:Time(ii);
y{ii} = exp(-h(ii)*t{ii});
end
% Plot
t_ = [t{:}];
y_ = [y{:}];
figure;
plot(t_,y_);
axis([1 max(t_) -0.5 max(y_)+0.5]);
xlabel('Time');
ylabel('Rate');
end
It gives the following plot:

Plotting onto specified axes in Matlab GUIDE

I have the following code for a pushbutton using Matlab GUIDE.It is supposed to plot a rotating arrow which rotates to the angles specified by theta 1 and theta 2.
function start_pushbutton_callback_Callback(hObject, eventdata, handles)
% hObject handle to start_pushbutton_callback (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
theta1 = handles.xy_angle;
theta2 = handles.yz_angle;
delay = handles.speed;
axes(handles.axes_turntable_xy);
R= 1; %Radius of the turntable
E=[0;0;0];
C=[-2;0;0];% points on the X axis
D=[2;0;0]; % points on the X axis
G=[0;2;0]; % points on the Y axis
H=[0;-2;0];% points on the Y axis
for th =1:1:theta1
clf;
Radius =1;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal
L1= [R*cosd(th);R*sind(th);0];
drawvector(E,L1); % call the drawvector function, as previously
line([C(1),D(1)],[C(2),D(2)]);
line([G(1),H(1)],[G(2),H(2)]);
axis([-2 2 -2 2]);
grid on;
pause(delay);
end;
axes(handles.axes_turntable_yz) ;
R= 1; %Radius of the turntable
E=[0;0;0];
C=[-2;0;0];% points on the X axis
D=[2;0;0]; % points on the X axis
G=[0;2;0]; % points on the Y axis
H=[0;-2;0];% points on the Y axis
for th =1:1:theta2;
clf;
Radius = 1;
[x,y,z] = cylinder(Radius,200);
plot(x(1,:),y(1,:))
axis equal
L1= [R*cosd(th);R*sind(th);0];
drawvector(E,L1); % call the drawvector function
line([C(1),D(1)],[C(2),D(2)]);
line([G(1),H(1)],[G(2),H(2)]);
axis([-2 2 -2 2]);
grid on;
pause(delay);
end
However, I am facing two problems with this code:
1) It simply plots onto a new matlab figure instead of plotting it on axes_turntable_xy-
2) It displays the following error afer executing till the line axes(handles.axes_turntable_yz). The error is as follows:
Error using axes
Invalid object handle
Error in turntable_interface_model>start_pushbutton_callback_Callback (line 235)
axes(handles.axes_turntable_yz) ;
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in turntable_interface_model (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)turntable_interface_model('start_pushbutton_callback_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Can anyone help with this?
Okay, I debugged the code and this was the problematic line in the above code:
for th =1:1:theta1
clf;
Radius =1;
[x,y,z] = cylinder(Radius,200);
The working code is:
for th1 =1:1:theta1
cla(handles.axes_turntable_xy); %clears the figure handles.axes_turntable_xy
Radius =1;
[x,y,z] = cylinder(Radius,200);
This clears the figure I am plotting in in Matlab, not some unspecified figure! Although it's still hard to understand why Matlab was plotting these commands in a new figure inspite of making axes.turntable_xy the current axes.