[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math

Search:


View post   

>> No.5112969 [View]
File: 92 KB, 576x512, logo.png [View same] [iqdb] [saucenao] [google]
5112969

Post and discuss cool Matlab scripts. Include short description at top of code posts. If code is too long use pastebin? OC only.

I'll start slow, here is a 1D wave on a string you can play with your mouse:

clear all, close all; clc

c = 5; % wave speed
dx = .01;
dt = .01;
n = 101;
len = dx*(n - 1);
x = 0:dx:len; % x dim for plotting
y = 768; % screen pixel height
r2 = (c*dt/dx)^2;

Ko = diag(ones(1,n-1),-1) + diag(-2*ones(1,n),0) + diag(ones(1,n-1),1);
K = sparse(Ko*r2);
K([1,end],:) = 0;
A = (speye(n,n) - K);

uold = zeros(n,1);
ucurr = uold;
t = 0;

figure('Units','normalized','Position',[.1 .1 .8 .8])
while t <= 100
mxy = get(0,'PointerLocation');
ucurr(1) = 2*mxy(2)/y - 1;
unew = A\(2*ucurr - uold);
uold = ucurr;
ucurr = unew;
t = t + dt;

plot(x(1:end-1),unew(2:end))
axis([0 len -1 1]), getframe;
end

Navigation
View posts[+24][+48][+96]