Writing PDEs in MATLAB - matlab

I'm a completely new user to Matlab. I want to calculate PDEs like this one: PDE But I have no idea how to code it.
I have tried g(x)=diff(x,y)^2/(diff(x)*diff(y)); but it doesn't work.

By using the symbolic toolbox and diff() function the partial derivatives can be simplified as follows. I'm not sure if you'd like to solve any specific values or plots but going based on the image posted here are some techniques that may help.
Example Partial Derivatives of Function f Taken with Respect to X and Y:
%Creating symbolic variables/representations%
syms x y
%Function dependent on variables x and y%
f = 2*y*sin(5*x);
%Taking the partial derivative with respect to x%
g = diff(f,x);
%Taking the partial derivative with respect to y%
g = diff(g,y)
Example Equation:
Extension: Graphing
Plotting the original function and its derivative can be done by using the fsurf() function which plot symbolic functions.
%Plotting the symbolic result of the partial derivatives%
subplot(1,2,1); fsurf(f);
title('Original Function');
xlabel('X-Axis'); ylabel('Y-Label');
subplot(1,2,2); fsurf(g);
title('Derivative of Function');
xlabel('X-Axis'); ylabel('Y-Label');
Extension: Evaluating at Specific Values
%Evaluating partial derivative at specific values%
x = 1;
y = 1;
Answer = subs(g);
Answer_As_A_Double = double(Answer);
Using MATLAB version: R2019b

Related

Wolfram Alpha and MATLAB plot transfer function differently

I am trying to get the filter coefficients for a digital IIR filter of a simple 180° phase shift allpass filter with the transfer function: (1+s)/(1-s)
This is what Wolfram gives me:
Bode Plot in Wolfram
and this is what I get from MATLAB:
Bode Plot in MATLAB
My code is:
clc; clear; close all;
z = [-1]; %zeros
p = [1]; %poles
k = 1; %gain
[num,den] = zp2tf(z,p,k); %convert zero-pole into numerator denominator
freqz(num,den); %bode plot
I would like to get the same plot in MATLAB as I do in Wolfram Alpha, to obtain the filter coefficients with fvtool so I can write a filter in C. Therefore my question is how do manage to convert the poles and zeros of the transfer function into the right format so that MATLAB does the same plot like Wolfram Alpha? What am I doing wrong?
Your issue is that you are mixing concepts
freqz is for z-based discrete frequency transforms, while you are working with s-based continuous Laplace transforms. These are unequivocally not the same thing.
Just use the functions for continuous transforms.
z = [-1]; %zeros
p = [1]; %poles
k = 1; %gain
[num,den] = zp2tf(z,p,k); %convert zero-pole into numerator denominator
my_filter=tf(num,den);
bode(my_filter);

how do i plot hysteresis using matlab?

I want to model memristor using its equation in exponential mode using Matlab . My aim is to just get the hysteresis plot(i-v plot) in Matlab. the equation is like
i=x^n*b*sinh(av)+m(exp(gv)-1)
x'=Af(x)h(v)
where f(x) is window function and h(v) is polynomial function, A is constant.
f(x)=1-(2x-1)^2
h(v)=cv+dv^3;
c and d are constant c<0 & d>0
a=2; b=0.01; g=4; n=4; A=25; m=0.001;
x is internal state of device..is it possible to get plot in matlab?
I have tried in matlab it is showing errors
This script I found on google after a very brief search:
http://webee.technion.ac.il/people/skva/Memristor%20Models/MATLAB/memristor.m
You can choose there what kind of model do you want to use (Nonlinear Ion Drift model in your case), the type of window function (yours is Jogelkar's I assume) and nonlinear voltage-current relation.
After modifying a bit their script you should get something like this:
b=0.01; g=4; n=4; a=2;A=25; m=0.001;
c=-1;d=4;%your constants
numOfPoints = 10000;
t = linspace(-1, 1,numOfPoints);
dt = t(2) - t(1);
volt = .003*sin(2*pi*t);
x = zeros(size(t));
curr = x;
for i=2:numOfPoints
x(i) = x(i-1) + A*(c*volt(i)+d*(volt(i))^3)*(1-(2*x(i-1)-1)^2)*dt;
curr(i)=x(i)^n*b*sinh(a*volt(i))+m*(exp(g*volt(i))-1);
end;
fig = figure(1);
plot(volt,curr);
xlabel('Voltage');ylabel('Current');
figure(fig);
Good luck!

Custom histogram density evaluation in MatLab

Does MatLab have any built in function to evaluate the density of a random variable from a custom histogram? (I suspect there are probably lots of ways to do this, I am just looking to see if there is already any builtin MatLab functionality).
Thanks.
The function hist gives you an approximation of the probability density you are evaluating.
If you want a continuous representation of it, this article from the Matlab documentation explains how to get one using the spline command from the Curve Fitting Toolbox. Basically the article explains how to make a cubic interpolation of your histogram.
The resulting code is :
y = randn(1,5001); % Replace y by your own dataset
[heights,centers] = hist(y);
hold on
n = length(centers);
w = centers(2)-centers(1);
t = linspace(centers(1)-w/2,centers(end)+w/2,n+1);
dt = diff(t);
Fvals = cumsum([0,heights.*dt]);
F = spline(t, [0, Fvals, 0]);
DF = fnder(F);
hold on
fnplt(DF, 'r', 2)
hold off
ylims = ylim;
ylim([0,ylims(2)]);
A popular way is to use kernel density estimation. The simplest way to do this in Matlab is using ksdensity.

Returning original mathematical function values from ode45 in Matlab?

My intention is to plot the original mathematical function values from the differential equation of the second order below:
I(thetadbldot)+md(g-o^2asin(ot))sin(theta)=0
where thetadbldot is the second derivative of theta with respect to t and m,d,I,g,a,o are given constants. Initial conditions are theta(0)=pi/2 and thetadot(0)=0.
My issue is that my knowledge and tutoring is limited to storing the values of the derivatives and returning them, not values from the original mathematical function in the equation. Below you can see a code that calculates the differential in Cauchy-form and gives me the derivatives. Does anyone have suggestions what to do? Thanks!
function xdot = penduluma(t,x)
% The function penduluma(t,x) calculates the differential
% I(thetadbldot)+md(g-o^2asin(ot))sin(theta)=0 where thetadbldot is the second
% derivative of theta with respect to t and m,d,I,g,a,o are given constants.
% For the state-variable form, x1=theta and x2=thetadot. x is a 2x1 vector on the form
% [theta,thetadot].
m=1;d=0.2;I=0.1;g=9.81;a=0.1;o=4;
xdot = [x(2);m*d*(o^2*a*sin(o*t)-g)*sin(x(1))/I];
end
options=odeset('RelTol', 1e-6);
[t,xa]=ode45(#penduluma,[0,20],[pi/2,0],options);
% Then the desired vector from xa is plotted to t. As it looks now the desired
% values are not found in xa however.
Once you have the angle, you can calculate the angular velocity and acceleration using diff:
options=odeset('RelTol', 1e-6);
[t,xa]=ode45(#penduluma,[0,20],[pi/2,0],options);
x_ddot = zeros(size(t));
x_ddot(2:end) = diff(xa(:,2))./diff(t);
plot(t,xa,t,x_ddot)
legend('angle','angular velocity','angular acceleration')
which gives the following plot in Octave (should be the same in MATLAB):
Alternatively, you can work it out using your original differential equation:
x_ddot = -m*d*(o^2*a*sin(o*t)-g).*sin(xa(:,1))/I;
which gives a similar result:

How to plot a bessel-like function in MATLAB

I'm totally new to MATLAB and I know only few basic commands. My task is to plot a function of this kind:
I(T) = ((2*J(k*r*sin(T))/(k*r*sin(T))))^2
where
T = angle
k = (2*pi*f)/c (f= frequency in Hz and c is speed of sound)
r = radius
J = bessel function first kind
I explain a bit: the function represent the power of a soundwave in the space. I've tried many times to plot this but i get always a single point in the plot.
I assume you've defined your Bessel function in J. If not, the MATLAB command for a Bessel function of the first kind is besselj. You'll also have to specify the order of the Bessel function.
You can define your anonymous function as
f=#(t,k,r)(2*besselj(0,k*r*sin(t))./(k*r*sin(t))).^2
and plot it as
T=linspace(0,pi,100);%# a sample angle vector
plot(T,f(T,k,r)) %# where k and r are values you'll have to provide
what about
I = ((2*J(k*r*sin(T))./(k*r*sin(T)))).^2
I finally found how to manage the problem described above, here's the solution that i found, in case that other people may need it.
% MATLAB Istruction for graph generation of bessel like function %
% Variables (fixed values)%
k = 912.91
r = 0.0215
% Set the range for the angle theta %
theta = (-(2/3)*pi:pi/180:(2/3)*pi)
% Calculation of bessel function of first kind %
J = besselj(1,k*r*sin(theta))
% Calculation I function %
% notice the ./ and .^ operators
I = ((2*J)./(k*r*sin(theta))).^2
% now plot the results with the plot command
plot(theta,I)