Using the RungeKutta integration method in a system
h=0.005;
x = 0:h:40;
y = zeros(1,length(x));
y(1) = 0;
F_xy = ;
for i=1:(length(x)1)
k_1 = F_xy(x(i),y(i));
k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h;
end
I have the following code, I think it's right. I know there's parts missing on the F_xy because this is my follow up question.
I have dx/dt = = −x(2 − y) with t_0 = 0, x(t_0) = 1
and dy/dt = y(1 − 2x) with t_0 = 0, y(t_0) = 2.
My question is that I don't know how to get these equations in to the code. All help appreciated
1 answer

Is
F_xy
your derivative function?If so, simply write it as a helper function or function handle. For example,
F_xy=@(x,y)[x*(2y);y*(12*x)];
Also note that your
k_1, k_2, k_3, k_4, y(i)
are all twodimensional. You need to resize youry
and rewrite the indices in your iterating steps accordingly.
See also questions close to this topic

count number object in binary picture on
I need some help , the task is to count black shapes. enter image description here
The algorithm for segmentation is as follows:
val=1 for x=1:no. of column for y=1:no. of rows if image(x,y)= 0 /* if a shape point */ segment(image, val, x, y) ; val=val+1 ; function segment(image, val, x, y) image(x,y)=val ; for i=1:1 for j=1:1 if image(x+i,y+j)=0 /* if a shape point */ segment(image,value,x+i,y+j) end function /* After the call to the function, all the pixels of the shape are marked by the values val, and by this way it is separated from the other shapes and background. These points are not tested again (they are no longer zero)
My code make me an eror , the code is :
global image
val=1 image=imread('cell.bmp'); %load picture %imshow(image)%show image %size(image) %view the length and with op the picture
for x=1:no. of column for y=1:no. of rows if image(x,y)= 0 /* if a shape point */ segment(image, val, x, y) ; val=val+1 ; function segment(image, val, x, y) image(x,y)=val ; for i=1:1 for j=1:1 if image(x+i,y+j)=0 /* if a shape point */ segment(image,value,x+i,y+j) end function
Thanks

Matlab function input argument parsing
I am going to define a function like this:
output = myFunc(para1,varargin) para2 = varargin{1} %numerical value para3Name = varargin{2} %parameter name para3Val = varargin{3} %parameter value
I tried using
addOptional
for parsing the parametervalue pair:ip = inputParser; ip.addOptional('para3Name',0, @(n) validateattributes(n,{'double'},{'integer','real','scalar'})); ip.parse(varargin{:}); res = ip.Results;
But it gave a running error when implementing something like:
myFunc(para1,para2,'para3Name',para3Val)
Error:
The value of 'para3Name' is invalid. Expected input to be integervalued.
It seems to be because the first optional argument was not clearly defined yet.
How to set up the input parsing to differentiate the first optional argument (a single value that does not involve a parameter name) and the second & third parametervalue pair?

How to trigger an alarm while my matlab GUI acquire and plot a real time signal?
I'm trying to make a GUI on matlab that read and plot a real time signal and, if the signal overcome a threshold for a specific time (threshold and time are imposed by the user) I want to trigger an alarm. The signal is acquired with arduino, and the acquisition and the plot work well. The problem is that: if I try to control the value of the sample that I acquired, the plot stops. I want to plot the real time data and at the same time control the value. The alarm can be a simple text that blink in an edit box and a sound or whatever is better in your opinion and even if the alarm is going on, the plot has to still work. That's the code;
%  Executes on button press in read_signal. function read_signal_Callback(hObject, eventdata, handles) % hObject handle to read_signal (see GCBO) % eventdata reserved  to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) cla reset fs=1; % ROUTINE FOR REAL TIME READING % create arduino object delete(instrfind) a = serial('COM5'); %define serial port a.BaudRate=115200; %define baud rate %open serial port fopen(a); k = 0; %index v = 0; %voltage t = 0; %time axes(handles.axes1) plotGraph=plot(t,v); xlabel ('Time (s)'), ylabel('Voltage'); axis([0 30 0 5]); scrollWidth=30; %window width delay=1/fs; %distance between two samples tic % Start timer while(1) threshold_im=str2double(get(handles.threshold,'String')); time_im= str2double(get(handles.time,'String')); k=k+1; v_read=fscanf(a,'%s'); %read arduino t(k) = toc; %Extract Elapsed Time v(k)=str2double(v_read); th_y(k)=threshold_im; date=clock; A=[date v(k) time_im threshold_im];%A is vector with date (minute resolution), voltage amplitude (V), time over threshold imposed and threshold % Writing the signal value with data attached in a file fileID=fopen('pastsignal.txt','a'); fprintf(fileID,'\n%.0f %.0f %.0f %.0f %.0f %.0f %.2f %.2f %.2f\r\n', A(1),A(2),A(3),A(4),A(5),A(6),A(7),A(8),A(9)); fclose(fileID); if(scrollWidth > 0) plotGraph=plot(t(t > t(k)scrollWidth),v(t > t(k)scrollWidth),'b'); hold on plot(t(t > t(k)scrollWidth),th_y(t > t(k)scrollWidth),'r') axis([t(k)scrollWidth t(k) 0 5]); else plotGraph=plot(t,v,'b'); hold on plot(t,th_y,'r') axis([0 t(k) 0 5]); end %Allow MATLAB to Update Plot pause(delay); end % close the serial port! fclose(a);

Backsubstitution in Python for 3x3 matrix
import numpy as np A = np.matrix([[1,1,1],[0,1,3],[0,0,6]]) b = np.array([9,3,8]) def back_sub(A,b): n = len(A) print('n is:', n) x = [0]*n for i in range(n1,1,1): #this refers to the rows; i goes 2,1,0 for j in range(i+1,n): b[i] = b[i]  A[i,j]*x[j] x[i] = b[i]/A[i,i] return x
I am trying to back substitute an uppertriangular matrix A, and come up with an answer: x1=4/3,x2=7,x3=−4/3 .
When I run my code, it gives correct answer for x2 and x3 , but x1 comes out as 0, which is confounding. I have tried to debug my code, and it seems to run fine until i=0 . Why isn't this code giving me the correct value of x1 ?

LaxWendroff order of accuracy on Burgers equation
Statement of the problem
Is it possible to achieve the second order of convergence (OOC) of LaxWendroff scheme applied to solve inviscid Burgers equations with discontinuous initial data? If no, then how to achieve OOC of 2nd order in quasilinear problem?
Wolfram Mathematica code
(*Initial data*) u0[x_] := 1  UnitStep[x  0.1]; Flux[u_] := 0.5 u^2; u = u0[xTbl]; dt = Abs[\[Sigma]] /Abs[Max[u]] dx; un = 0*u; F = Flux[u]; (*LW*) t = 0; While[t < tFin, (*Main loop*) un[[2 ;; nx  1]] = u[[2 ;; nx  1]]  0.50 \[Sigma] (F[[3 ;; nx]]  F[[1 ;; nx  2]]) + 0.25 \[Sigma]^2 ((u[[3 ;; nx]] + u[[2 ;; nx  1]]) (F[[3 ;; nx]]  F[[2 ;; nx  1]])  (u[[2 ;; nx  1]] + u[[1 ;; nx  2]]) (F[[2 ;; nx  1]]  F[[1 ;; nx  2]])); (*BC*) un[[1]] = u[[1]]; un[[nx]] = u[[nx]]; (*Update*) u = un; F = Flux[u]; nstep = nstep + 1; t = t + dt ];
Output
nx = {50, 100, 200, 400, 800}; L1err = {0.0217352, 0.0107321, 0.00533915, 0.00207726, 0.00132978}; p = {1.0181, 1.00725, 1.36192, 0.643502}
Average OOC equals
1.00769

how many IEEE doubleprecision floatingpoint numbers can be represented in the open interval (511,512)
[Question]
Here is the question. I know how to write it in terms of powers of two.

Volume of 3d shape using numerical integration with scipy
I have written a function for computing volume of intersection of a cube and a halfspace and now I'm writing tests for it.
I've tried computing the volume numerically like this:
integral = scipy.integrate.tplquad(lambda z, y, x: int(Vector(x, y, z).dot(normal) < distance), 0.5, 0.5, lambda x: 0.5, lambda x: 0.5, lambda x, y: 0.5, lambda x, y: 0.5, epsabs=1e5, epsrel=1e5)
... basically I integrate over the whole cube and each point gets value 1 or 0 based on if it is inside the half space. This gets very slow (more than several seconds per invocation) and keeps giving me warnings like
scipy.integrate.quadpack.IntegrationWarning: The integral is probably divergent, or slowly convergent
Is there a better way to calculate this volume?

Calculate Arc Length, Area and Volume using Pari/GP
I'm new to PARI/GP, and I was wondering how to calculate the arc length, area and volume indicated by some complicated implicit function (for example, the nellipse function)? I have tried using Mathematica but it is too slow to yield some numerical result. Thank you.

Performing a complicated quadruple integral in MATLAB
I am trying to create a function that computes this integral,
where the variables a, w, and N can be specified by the user.
Upon reading other forums, it is recommended to embed an integral3 function within another integral as I have done
function L=selfinductionr(a, wring, N) syms zed z_prime theta theta_prime thetamin=0; zmin=wring/2; thetamax=2*pi/N; zmax=wring/2; L=(10^(7)/wring^2)*integral(@(theta_prime)integral3(@(zed,z_prime,theta)... func,zmin,zmax,zmin,zmax,thetamin,thetamax)... ,thetamin,thetamax,'ArrayValued',true); %(H) function L=func(zed,z_prime,theta,theta_prime) L=a^2*cos(theta_prime  theta)/... sqrt((a*cos(theta)a*cos(theta_prime))^2 ... +(a*sin(theta)a*sin(theta_prime))^2+(zedz_prime)^2); end end
However I appear to be having an error with using syms and attempting to "add zed to a static workspace". Any advice would be massively appreciated.