I'm pretty new to MATLAB and trying to reverse a conversion from an altitude to a Reynold's number.
The function altitude -> Reynold's number is defined section wise, because different methods apply, depending on the altitude being higher or lower than a threshold.
The conversion is given by this function:
function [Re] = convertAltToRe(h, Ma, L)
%% Constants
h_trop = 11000; % [m] - ISA altitude of tropopause
rho_0 = 1.225; % [kg/m^3] - Standard density of air
T_0 = 288.15; % [K] - ISA Reference Temperature
T_tropo = 216.65; % [K] - Reference temperature for 11,000m < h < 20,000m
T_s = 273.15; % [K] - Standard temperature
dT = -0.0065; % [K/m] - Temperature gradient up to 11,000m
k = 1.4; % [-] - Isentropic exponent dry air
R = 287.058; % [J/(K*kg)] - Specific gas constant dry air
my_0 = 1.716e-005; % [Pa*s] - Dynamic viscosity at sealevel
C = 110.4; % [K] - Sutherland constant dry air
g = 9.80665; % [m/s^2] - Gravitational accerleration
%% TAS from speed of sound
if h < h_trop
c_s = sqrt((T_0+(dT*h))*k*R);
else
c_s = sqrt(T_tropo*k*R);
end
v = Ma * c_s;
%% Dynamic viscosity
beta = my_0*(T_s+C)/(T_s^1.5);
T = T_0 + dT*h;
if h < h_trop
my = beta*T^(1.5)/(T+C);
else
my = beta*T_tropo^(1.5)/(T_tropo+C);
end
%% Density
if h < h_trop
rho = rho_0*(1+(dT*h/T_0))^(-g/R/dT-1);
else
rho = rho_0*(T_tropo/T_0)^(-g/R/dT-1)*exp(-g/R/T_tropo*(h-h_trop));
end
%% Reynold's number
Re = rho*v*L/my;
end
Now I try to find the altitude for a given Ma, L and Re.
My attempt looks like this:
Re = 2.693844519000000e+07;
syms f;
syms x real;
f = convertAltToRe(x, 0.78,4.371623535) == Re;
res = vpasolve(f,x,10000);
The error message says that the operation h < h_trop is not possible, because it is a conversion from a symbolic variable to a locigal value.
Is it possible to solve an equation of this form?
1.- There seems to be a zero that can be found with min
Re = 2.693844519000000e+07;
dh=[0:.01:2e4];
Ma=0.78
L=4.371623535
h=alttore(dh,Ma,L);
h_dB=10*log10(abs(h-Re));
figure
plot(dh,h_dB)
grid on
[mval,mind]=min(h_dB)
h0=alttore(dh(mind),Ma,L)
h0 =
2.662955072438022e+07
2.- Narrowing dh window one increases resulting h0 accuracy
dh=[1:.0001:1.2e4];
h=alttore(dh,Ma,L);
h_dB=10*log10(abs(h-Re));
[mval,mind]=min(h_dB)
h0=alttore(dh(mind),Ma,L)
h0 =
2.662954575557600e+07
Related
Find the error as a function of n, where the error is defined as the difference between two the voltage from the Fourier series (vF (t)) and the value from the ideal function (v(t)), normalized to the maximum magnitude (Vm ):
I am given this prompt where Vm = 1 V. Below this line is the code which I have written.
I am trying to write a function to solve this question: Plot the error versus time for n=3,n=5,n=10, and n=50. (10points). What does it look like I am doing incorrectly?
clc;
close all;
clear all;
% define the signal parameters
Vm = 1;
T = 1;
w0 = 2*pi/T;
% define the symbolic variables
syms n t;
% define the signal
v1 = Vm*sin(4*pi*t/T);
v2 = 2*Vm*sin(4*pi*t/T);
% evaluate the fourier series integral
an1 = 2/T*int(v1*cos(n*w0*t),0,T/2) + 2/T*int(v2*cos(n*w0*t),T/2,T);
bn1 = 2/T*int(v1*sin(n*w0*t),0,T/2) + 2/T*int(v2*sin(n*w0*t),T/2,T);
a0 = 1/T*int(v1,0,T/2) + 1/T*int(v2,T/2,T);
% obtain C by substituting n in c[n]
nmax = 100;
n = 1:nmax;
a = subs(an1);
b = subs(bn1);
% define the time vector
ts = 1e-2; % ts is sampling the
t = 0:ts:3*T-ts;
% directly plot the signal x(t)
t1 = 0:ts:T-ts;
v1 = Vm*sin(4*pi*t1/T).*(t1<=T/2);
v2 = 2*Vm*sin(4*pi*t1/T).*(t1>T/2).*(t1<T);
v = v1+v2;
x = repmat(v,1,3);
% Now fourier series reconstruction
N = [3];
for p = 1:length(N)
for i = 1:length(t)
for k = N(p)
x(k,i) = a(k)*cos(k*w0*t(i)) + b(k)*sin(k*w0*t(i));
end
% y(k,i) = a0+sum(x(:,i)); % Add DC term
end
end
z = a0 + sum(x);
figure(1);
plot(t,z);
%Percent error
function [per_error] = percent_error(measured, actual)
per_error = abs(( (measured - actual) ./ 1) * 100);
end
The purpose of the forum is helping with specific technical questions, not doing your homework.
This is the lbp function of a facial expression recognition project that I'd created. The matlab version that this was created in as 2013. I am currently running this on 2017a.
Here is the function that I'm trying to run:
% result = lbp(I) returns the LBP histogram of the image I
function result = lbp(image)
d_image=double(image);
radius=1;
neighbours=8;
neighbour_points=zeros(neighbours,2);
% Angle step.
a = 2*pi/neighbours;
for i = 1:neighbours
neighbour_points(i,1) = -radius*sin((i-1)*a);
neighbour_points(i,2) = radius*cos((i-1)*a);
end
% Determine the dimensions of the input image.
[m1,n1]=size(image);
miny=min(neighbour_points(:,1));
maxy=max(neighbour_points(:,1));
minx=min(neighbour_points(:,2));
maxx=max(neighbour_points(:,2));
blocksizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
blocksizex=ceil(max(maxx,0))-floor(min(minx,0))+1;
% Coordinates of origin (0,0) in the block
origy=1-floor(min(miny,0));
origx=1-floor(min(minx,0));
% Minimum allowed size for the input image depends
% on the radius of the used LBP operator.
if(n1 < blocksizex || m1 < blocksizey)
error('Small input image. Must be at least (2*radius+1) x (2*radius+1)');
end
% Calculate dx and dy;
dx = n1 - blocksizex;
dy = m1 - blocksizey;
% Fill the center pixel matrix C.
C = image(origy:origy+dy,origx:origx+dx);
%disp(C);
d_C = double(C);
% Initialize the result matrix with zeros.
result=zeros(dy+1,dx+1);
%Computing the LBP code image
for i = 1:neighbours
y = neighbour_points(i,1)+origy;
x = neighbour_points(i,2)+origx;
% Calculate floors, ceils and rounds for the x and y.
fy = floor(y); cy = ceil(y); ry = round(y);
fx = floor(x); cx = ceil(x); rx = round(x);
% Check if interpolation is needed.
if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)
% Interpolation is not needed, use original datatypes
N = image(ry:ry+dy,rx:rx+dx);
D = N >= C;
else
% Interpolation needed, use double type images
ty = y - fy;
tx = x - fx;
% Calculate the interpolation weights.
w1 = roundn((1 - tx) * (1 - ty),-6);
w2 = roundn(tx * (1 - ty),-6);
w3 = roundn((1 - tx) * ty,-6);
% w4 = roundn(tx * ty,-6) ;
w4 = roundn(1 - w1 - w2 - w3, -6);
% Compute interpolated pixel values
N = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);
N = roundn(N,-4);
D = N >= d_C;
end
% Update the result matrix.
v = 2^(i-1);
result = result + v*D;
if i==neighbours
end
end
load('binvector.mat');
bins = 59;
for i = 1:size(result,1)
for j = 1:size(result,2)
result(i,j)=table(result(i,j)+1);
end
end
% calculating histogram
result=hist(result(:),0:(bins-1));
end
function x = roundn(x, n)
if n<0
p=10 ^ -n;
x=round(p*x)/p;
elseif n>0
p=10^n;
x=p*round(x/p);
else
x=round(x);
end
end
And here is the error that popped up:
Error using tabular/double (line 148)
Undefined function 'double' for input arguments of type 'table'. To convert to numeric, use the TABLE2ARRAY function, or
extract data using dot or brace subscripting.
Error in lbp (line 104)
result(i,j)=table(result(i,j)+1);
I havent touched matlab in a long while, and so not sure how to tackle this, or what a suitable replacement method would be. Any input would be appreciated.
I want to write a program that makes use of Newtons Method:
To estimate the x of this integral:
Where X is the total distance.
I have functions to calculate the Time it takes to arrive at a certain distance by using the trapezoid method for numerical integration. Without using trapz.
function T = time_to_destination(x, route, n)
h=(x-0)/n;
dx = 0:h:x;
y = (1./(velocity(dx,route)));
Xk = dx(2:end)-dx(1:end-1);
Yk = y(2:end)+y(1:end-1);
T = 0.5*sum(Xk.*Yk);
end
and it fetches its values for velocity, through ppval of a cubic spline interpolation between a set of data points. Where extrapolated values should not be fetcheable.
function [v] = velocity(x, route)
load(route);
if all(x >= distance_km(1))==1 & all(x <= distance_km(end))==1
estimation = spline(distance_km, speed_kmph);
v = ppval(estimation, x);
else
error('Bad input, please choose a new value')
end
end
Plot of the velocity spline if that's interesting to you evaluated at:
dx= 1:0.1:65
Now I want to write a function that can solve for distance travelled after a certain given time, using newton's method without fzero / fsolve . But I have no idea how to solve for the upper bound of a integral.
According to the fundamental theorem of calculus I suppose the derivative of the integral is the function inside the integral, which is what I've tried to recreate as Time_to_destination / (1/velocity)
I added the constant I want to solve for to time to destination so its
(Time_to_destination - (input time)) / (1/velocity)
Not sure if I'm doing that right.
EDIT: Rewrote my code, works better now but my stopcondition for Newton Raphson doesnt seem to converge to zero. I also tried to implement the error from the trapezoid integration ( ET ) but not sure if I should bother implementing that yet. Also find the route file in the bottom.
Stop condition and error calculation of Newton's Method:
Error estimation of trapezoid:
Function x = distance(T, route)
n=180
route='test.mat'
dGuess1 = 50;
dDistance = T;
i = 1;
condition = inf;
while condition >= 1e-4 && 300 >= i
i = i + 1 ;
dGuess2 = dGuess1 - (((time_to_destination(dGuess1, route,n))-dDistance)/(1/(velocity(dGuess1, route))))
if i >= 2
ET =(time_to_destination(dGuess1, route, n/2) - time_to_destination(dGuess1, route, n))/3;
condition = abs(dGuess2 - dGuess1)+ abs(ET);
end
dGuess1 = dGuess2;
end
x = dGuess2
Route file: https://drive.google.com/open?id=18GBhlkh5ZND1Ejh0Muyt1aMyK4E2XL3C
Observe that the Newton-Raphson method determines the roots of the function. I.e. you need to have a function f(x) such that f(x)=0 at the desired solution.
In this case you can define f as
f(x) = Time(x) - t
where t is the desired time. Then by the second fundamental theorem of calculus
f'(x) = 1/Velocity(x)
With these functions defined the implementation becomes quite straightforward!
First, we define a simple Newton-Raphson function which takes anonymous functions as arguments (f and f') as well as an initial guess x0.
function x = newton_method(f, df, x0)
MAX_ITER = 100;
EPSILON = 1e-5;
x = x0;
fx = f(x);
iter = 0;
while abs(fx) > EPSILON && iter <= MAX_ITER
x = x - fx / df(x);
fx = f(x);
iter = iter + 1;
end
end
Then we can invoke our function as follows
t_given = 0.3; % e.g. we want to determine distance after 0.3 hours.
n = 180;
route = 'test.mat';
f = #(x) time_to_destination(x, route, n) - t_given;
df = #(x) 1/velocity(x, route);
distance_guess = 50;
distance = newton_method(f, df, distance_guess);
Result
>> distance
distance = 25.5877
Also, I rewrote your time_to_destination and velocity functions as follows. This version of time_to_destination uses all the available data to make a more accurate estimate of the integral. Using these functions the method seems to converge faster.
function t = time_to_destination(x, d, v)
% x is scalar value of destination distance
% d and v are arrays containing measured distance and velocity
% Assumes d is strictly increasing and d(1) <= x <= d(end)
idx = d < x;
if ~any(idx)
t = 0;
return;
end
v1 = interp1(d, v, x);
t = trapz([d(idx); x], 1./[v(idx); v1]);
end
function v = velocity(x, d, v)
v = interp1(d, v, x);
end
Using these new functions requires that the definitions of the anonymous functions are changed slightly.
t_given = 0.3; % e.g. we want to determine distance after 0.3 hours.
load('test.mat');
f = #(x) time_to_destination(x, distance_km, speed_kmph) - t_given;
df = #(x) 1/velocity(x, distance_km, speed_kmph);
distance_guess = 50;
distance = newton_method(f, df, distance_guess);
Because the integral is estimated more accurately the solution is slightly different
>> distance
distance = 25.7771
Edit
The updated stopping condition can be implemented as a slight modification to the newton_method function. We shouldn't expect the trapezoid rule error to go to zero so I omit that.
function x = newton_method(f, df, x0)
MAX_ITER = 100;
TOL = 1e-5;
x = x0;
iter = 0;
dx = inf;
while dx > TOL && iter <= MAX_ITER
x_prev = x;
x = x - f(x) / df(x);
dx = abs(x - x_prev);
iter = iter + 1;
end
end
To check our answer we can plot the time vs. distance and make sure our estimate falls on the curve.
...
distance = newton_method(f, df, distance_guess);
load('test.mat');
t = zeros(size(distance_km));
for idx = 1:numel(distance_km)
t(idx) = time_to_destination(distance_km(idx), distance_km, speed_kmph);
end
plot(t, distance_km); hold on;
plot([t(1) t(end)], [distance distance], 'r');
plot([t_given t_given], [distance_km(1) distance_km(end)], 'r');
xlabel('time');
ylabel('distance');
axis tight;
One of the main issues with my code was that n was too low, the error of the trapezoidal sum, estimation of my integral, was too high for the newton raphson method to converge to a very small number.
Here was my final code for this problem:
function x = distance(T, route)
load(route)
n=10e6;
x = mean(distance_km);
i = 1;
maxiter=100;
tol= 5e-4;
condition=inf
fx = #(x) time_to_destination(x, route,n);
dfx = #(x) 1./velocity(x, route);
while condition > tol && i <= maxiter
i = i + 1 ;
Guess2 = x - ((fx(x) - T)/(dfx(x)))
condition = abs(Guess2 - x)
x = Guess2;
end
end
I am trying to model a PDE including advection-diffusion and reaction terms. I want to see the concentration profile after a long time (2*60*60 sec) in a very tiny slab (5 micrometer). I used method of line (ode15s) to solve this PDE but my result does not match with the experimental result. Because the outlet concentration gets maximum concentration right away (as can be seen in my plot). Does anybody have a suggestion?
clc
clear
close all
%%%%%%%%%%%parameters%%%%%%%%%%%%%%%%
global a b g Ki dx cin
Lf=5e-6; %microM
dm=8.6*10^-6; %m2/min
area=0.65; % m^-1
cin=20; %
ki=1; %mg/m3 min
Ki=0.24; %m3/mg
ux=0.5; %m/min
%%%%%%%%%%%%%%%%%%constant%%%%%%%%%%%%%%%%%%%
a=dm;
b=ux;
g=ki*Ki;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tf=2*60*60;
nx=50;
xf=Lf;
dx=xf/nx;
nt=1000;
dt=tf/nt;
x = 0:dx:xf;
t = 0:dt:tf;
N = numel(x);
u0 = zeros(N,1);
y0 = u0 ;
[t,y] = ode15s(#(t,y)fun(t,y,x,N),t,y0);
u = y(:,1:2:end);
x = (1:N)/(N+1);
plot(t/60,u(:,end))
function dydt=fun(~,y,~,N)
global a b g dx cin
bc=cin;
dydt = zeros(N,1);
i = 1;
dydt(i,:)=a*(y(i+1,:)-2*y(i,:)+bc)/(dx^2)-b*(y(i+1,:)-bc)./(2*dx)-g.*y(i+1,:);
i = 2:N-1;
dydt(i,:)=a*(y(i+1,:)-2*y(i,:)+y(i-1,:))/(dx^2)-b*(y(i+1,:)-y(i-1,:))./(2*dx)-g.*y(i,:);
i = N;
dydt(i,:)=a*(y(N,:)-2*y(i,:)+y(i-1,:))/(dx^2)-b*(y(N,:)-y(i-1,:))./(2*dx)-g.*y(i,:);
end
I have a Matlab code that simulates frisbee flight dynamics. I would like to add a wind variable. I did it, but after seeing the plots I think my wind is reducing the speed of the disc. I mean it should change the speed of the disc but via lift and drag force, now it looks like wind speed variable direcly changes disc speed variable. What I want is to affect only the lift and drag forces with wind, but I can't make it work. Here is my current code that is not working. This is an external M-file which is used by the ode45 function in the main script:
[t,x]=ode45(#discfltEOM,tspan,x0,options,CoefUsed);
function xdot=discfltEOM(t,x,CoefUsed)
% Equations of Motion for the frisbee
% The inertial frame, xyz = forward, right and down
global m g Ia Id A d rho
global CLo CLa CDo CDa CMo CMa CRr
global CL_data CD_data CM_data CRr_rad CRr_AdvR CRr_data
global CMq CRp CNr
% x = [ x y z vx vy vz f th fd thd gd gamma Wx Wy]
% 1 2 3 4 5 6 7 8 9 10 11 12 13 14
%% give states normal names
vx = x(4);
vy = x(5);
vz = x(6);
f = x(7);
th = x(8);
st = sin(th);
ct = cos(th);
sf = sin(f);
cf = cos(f);
fd = x(9);
thd= x(10);
gd = x(11);
Wx = x(13);
Wy = x(14);
%% Define transformation matrix
%% [c]=[T_c_N] * [N]
T_c_N=[ct st*sf -st*cf; 0 cf sf; st -ct*sf ct*cf];
%% [d]=[T_d_N] * [N]
%T_d_N(1,:)=[cg*ct sg*cf+sf*st*cg sf*sg-st*cf*cg];
%T_d_N(2,:)=[ -sg*ct cf*cg-sf*sg*st sf*cg+sg*st*cf];
%T_d_N(3,:)=[ st -sf*ct cf*ct]
[evec,eval]=eig(T_c_N);
eigM1=diag(eval);
m1=norm(eigM1(1));
m2=norm(eigM1(2));
m3=norm(eigM1(3));
c1=T_c_N(1,:); % c1 expressed in N frame
c2=T_c_N(2,:); % c2 expressed in N frame
c3=T_c_N(3,:); % c3 expressed in N frame
%% calculate aerodynamic forces and moments
%% every vector is expressed in the N frame
vel = [vx vy vz]; %expressed in N
vmag = norm(vel);
Vwiatr = [Wx Wy 0];
Vw = norm(Vwiatr);
vc3=dot(vel,c3); % velocity (scalar) in the c3 direction
vp= [vel-vc3*c3]; % subtract the c3 velocity component to get the velocity vector
% projected onto the plane of the disc, expressed in N
alpha = atan(vc3/norm(vp));
Adp = A*rho*(vmag-Vw)*(vmag-Vw)/2;
uvel = vel/vmag; % unit vector in vel direction, expressed in N
uvp = vp/norm(vp); % unit vector in the projected velocity direction, expressed in N
ulat = cross(c3,uvp); % unit vec perp to v and d3 that points to right, right?
%% first calc moments in uvp (roll), ulat(pitch) directions, then express in n1,n2,n3
omegaD_N_inC = [fd*ct thd fd*st+gd]; % expressed in c1,c2,c3
omegaD_N_inN = T_c_N'*omegaD_N_inC'; % expressed in n1,n2,n3
omegavp = dot(omegaD_N_inN,uvp);
omegalat = dot(omegaD_N_inN,ulat);
omegaspin = dot(omegaD_N_inN,c3); % omegaspin = p1=fd*st+gd
AdvR= d*omegaspin/2/vmag ; % advanced ration
if CoefUsed==1 % using short flights coefficients
CL = CLo + CLa*alpha;
alphaeq = -CLo/CLa; % this is angle of attack at zero lift
CD = CDo + CDa*(alpha-alphaeq)*(alpha-alphaeq);
CM=CMo + CMa*alpha;
%CRr= CRr*d*omegaspinv/2./vmagv';
%CRr= CRr*sqrt(d/g)*omegaspinv; % this line produces NaN, so leave it in Mvp equation
%Mvp = Adp*d* (CRr*d*omegaspin/2/vmag + CRp*omegavp)*uvp; % expressed in N
Mvp = Adp*d*(sqrt(d/g)*CRr*omegaspin + CRp*omegavp)*uvp; % expressed in N
end % if CoefUsed==1 % using short flights coefficients
if CoefUsed==2 % using potts coefficients
%% interpolation of Potts and Crowther (2002) data
CL = interp1(CL_data(:,1), CL_data(:,2), alpha,'spline');
CD = interp1(CD_data(:,1), CD_data(:,2), alpha,'spline');
CM = interp1(CM_data(:,1), CM_data(:,2), alpha,'spline');
CRr = interp2(CRr_rad,CRr_AdvR,CRr_data,alpha,AdvR,'spline');
Mvp = Adp*d* (CRr* + CRp*omegavp)*uvp; % Roll moment, expressed in N
end % if CoefUsed==2 % using potts coefficients
lift = CL*Adp;
drag = CD*Adp;
ulift = -cross(uvel,ulat); % ulift always has - d3 component
udrag = -uvel;
Faero = lift*ulift + drag*udrag; % aero force in N
FgN = [ 0 0 m*g]'; % gravity force in N
F = Faero' + FgN;
Mlat = Adp*d*(CM + CMq*omegalat)*ulat; % Pitch moment expressed in N
Mspin = [0 0 +CNr*(omegaspin)]; % Spin Down moment expressed in C
M = T_c_N*Mvp' + T_c_N*Mlat' + Mspin'; % Total moment expressed in C
% set moments equal to zero if wanted...
% M=[0 0 0];
% calculate the derivatives of the states
xdot = vel';
xdot(4) = (F(1)/m); %accx
xdot(5) = (F(2)/m); %accy
xdot(6) = (F(3)/m); %accz
xdot(7) = fd;
xdot(8) = thd;
xdot(9) = (M(1) + Id*thd*fd*st - Ia*thd*(fd*st+gd) + Id*thd*fd*st)/Id/ct;
xdot(10) = (M(2) + Ia*fd*ct*(fd*st +gd) - Id*fd*fd*ct*st)/Id;
fdd=xdot(9);
xdot(11) = (M(3) - Ia*(fdd*st + thd*fd*ct))/Ia;
xdot(12) = x(11);
xdot(13) = Wx;
xdot(14) = Wy;
xdott=xdot';
% calculate angular momentum
H = [Id 0 0 ; 0 Id 0; 0 0 Ia]*omegaD_N_inC';
format long;
magH = norm(H);
format short;
state=x';
Wx and Wy are wind vectors. I'm trying to affect the Adp variable because it is direcly connected with lift and drag. I made Wx = 1 [m/s] and the effect is immense, but should be very little. I'm terrible with Matlab so I'm sure I making some kind of stupid mistake from not understanding well how it all works.