/r/matlab
Official MATLAB subreddit
MATLAB news, code tips and tricks, questions, and discussion! We are here to help, but won't do your homework or help you pirate software.
The effort you put into asking a question is often matched by the quality of our answers.
Try saturnapi to share and run MATLAB code in a web browser!
If you want flair simply Message the mods
/r/matlab
Context.. I am trying to solve 4 PDE using matlab. The pde are interconnected and there are equation which connect dependent variables to eachh other too.. please help.
This is the code as of now generated .
a = 10; b = 10; Lp = 2b + 2(sqrt(b^2 + (api)^2))((3 + (2b/api)^2)/(4 + (2b/api)^2)); A = 2ab; Pa = 101325; % Pa Ta = 30; % Celsius D = (2.256/Pa)((Ta+273.13)/256)^1.81; V = 2; rhoa = 1.15; % kg/m^3 Sha = 2.45; Ky = rhoaShaDLp/4A; Nua = 2.45; ka = 0.0321; % W/(m K) h = NuakaLp/4A; had = 0.9; % J/kg K (specific heat of adsorption) fd = 0.914; % kg/m fm = 12; % kg/m cd = 0.85; % J/kg K cw = 4.19; % J/kg K cm = 0.9; % J/kg K ca = 1.005; % J/kg K cv = 2.028; % J/kg K
% Define spatial domain dz = 0.1; % spatial step size z = 0:dz:1; % spatial mesh
% Define weq function weq = @(z) z;
% Define q function q = @(RH) 0.133022 - 1.401746.*RH + 5.3785.*RH.^2 - 6.73709.*RH.^3 + 2.935.*RH.^4;
% Function for w dependence on Td and RH w_func = @(Td, RH) (0.62188*RH)/((Pa/exp(23.196-(3816/(Td-46.13)))));
% Discretize spatial derivatives [dw_dz, dq_dz, dT_dz, dTd_dz] = diff([zeros(size(z)); w0; q0; T0; Td0]) / dz;
% Define ODE system (anonymous function) f = @(t, y) [ -V * dw_dz - (fd / (2 * A * rhoa)) * dq_dz; -(2 * Ky * Pa / fd) * (weq(z) - y(2)); -V * dT_dz - (fd * (cd + q(y(3)) .* cw) + fm * cm) / ... (2 * A * rhoa * (ca + y(2) .* cv)) * dTd_dz + ... (Ky * Pa * had / (A * rhoa * (ca + y(2) .* cv))) * (y(2) - weq(z)); -2 * h * Lp / (fd * (cd + q(y(3)) .* cw) + fm * cm) * (y(4) - Ta) - ... (2 * Ky * Lp * had / (fd * (cd + q(y(3)) .* cw) + fm * cm)) * ... (weq(z) - y(2)) - ... (2 * Ky * Lp * cv / (fd * (cd + q(y(3)) .* cw) + fm * cm)) * ... (weq(z) - y(2)) * (y(4) - Ta) ];
% Solve the ODE system dt = 0.01; % time step size tspan = 0:dt:1; % time mesh
% Initial conditions w0 = zeros(size(z)); q0 = zeros(size(z)); T0 = zeros(size(z)); Td0 = zeros(size(z));
% Solve using ode45 [t, y] = ode45(f, tspan, [w0; q0; T0; Td0]);
% Extract solutions w = y(:, 2); q = y(:, 3); T = y(:, 4); Td = y(:, 5);
% Plot results figure; plot(z, w, 'r', z, q, 'g', z, T, 'b', z, Td, 'm'); xlabel('z'); ylabel('Solution'); legend('w', 'q', 'T', 'Td');
I was wondering if MATLAB is the better tool to pre and post process the images as compared to adobe photoshop. I want to automate the task and was thinking is MATLAB can give the same quality images.
In 2018a version, I have been trying to use the "to workspace" block in Simulink and been saving it as an array. However after I run the code in matlab for my circuit(i am trying to learn co simulation with Matlab script), the variable is not being displayed in the workspace. How to solve this problem?
I'm writing a MATLAB script to assign ratings to people with a certain batting average score. For example, if it was below .200, it would be Bad, .220 is Below Average etc. I wrote this code to loop over every player in a table, then every rating to find the score to assign:
ratings = {'Bad', 'Below Average', 'Average', 'Above average', 'Great', 'Elite'};
AVGRatings = [0.200, 0.220, 0.255, 0.280, 0.310, 0.340, inf];
for j = 1:numel(AVGRatings)
for i = 1:height(Batting)
if Batting.AVG(i) < AVGRatings(j)
Batting.AVG_Rating{i} = ratings{j};
break;
end
end
end
For some reason, it loops to the 6th iteration of the Batting table and then throws this error: Index exceeds the number of array elements. Index must not exceed 6.
Any help would be appreciated with this!
So my project has me organizing all of the information in the .dat file and outputting it to two .txt files. One that organizes by last name and one by their "quiz" average. I have my current code linked in this post and what my current .txt files look like. Does anyone have any suggestions on how to make it all readable and not look so messy. Any help would be greatly appreciated.
I used MATLAB’s ROS toolbox briefly, but only made a single node with publisher and subscriber components. I did not connect MATLAB to any hardware via ROS. Is it easy to do this? For instance, if I were to use a raspberry pi robot, is it feasible to configure that robot to subscribe to messages published by a node instantiated using MATLAB’s ROS toolbox?
I'm having trouble using fmincon to optimize an EM problem, specifically the synthesis of a pencil beam. Despite applying the function in different scenarios, I always get the same resault: 1.5dB more than the initial conditions of the problem. Has anyone had a similar issue?
Helloo, I am trying to identify the transfer function of my robot using the built in tool in matlab (system Identification). The problem I am facing is that I couldn't send the angles at a fixed sample rate to my computer via nrf24l01 modules, so I just sent the angles and the time they were measured to my computer and stored them in a txt file. The system Identification tool uses fixed sample rate to identify a system, so is it possible to identify my robot using an angles array and a time array? I am not a control theory student but I had a course about LTI systems.
Best courses to study simulink :- certificate will be there or any YouTube channel
Hello all the beautiful r/matlab community!
I happen to have modelled a VARX model from Econometric Modeler... is there a way to export it and make predictions with the model? Or from that same app you can make predictions?
Thank you very much !!!
This is output in Maxima. How can I make this in Matlab?
I tried disp but it can show one thing at the time
I only seem to be able to write a short vertical bar ('|') in an Equation in a Live Script. Is there some way to get the equivalent of LaTeX' "\left.\right|" (see example screenshot) or "\bigg|" in an Equation without having to resort to LaTeX?
Hello,
I'm solving an equation and the output is this:
4435179829497355625/18014398509481984 + 782041868234353125i/18014398509481984
And I need it to be written like this:
If anyone knows, I would be veryyyy grateful!!!!!
hello, this is sort of a homework question.
l just want to know what the k1 and k2 represent. l copied this pid controller design from youtube and l understand how it all works except for the k1 and k2 gains.
one is k1 is 2 and k2 is 100 and they are divided by the mass m
just want to know what are they?
thankyou in advance
solved: k1 = inertia and k2= damping
I am working on some code to take in some live data over serial, save it, plot it live and then export to CSV when I am done. Normally I use matlab for simulations and dynamic array haven't been an issue, but it looks like I'll be taking about 10 floats in, doing some conversions for storage a few times a second for a few minutes ( and maybe a few hours idle before depending how things go). With this scenario the dynamic array wont cut it (for speed and unknown allocation length). With this size of data some times it can take some time to process even with pre-allowcation and I don't want a stoppage in data collection.
What would be the best way to store the data. From the matlab documentation it look like tables might look similar to a fast data type dictionaries in python. Does anyone know the best approach for large data like this?
Tables looks promising and has has an objector oriented vib which could be helpful but it looks like its strength is processes large data quickly after it has been imported not for iteratively added in.
how can i do this in matlab?? https://thepythoncodingbook.com/2023/03/12/anatomy-2d-game-python-turtle-o/
This was inspired by the post "find string in 2d array without using cells" by u/Mark_Yugen.
Do you know you can use familiar math operators with string arrays? Check out this example.
ou want to create a 4-by-7 array of random numbers. Next your program moves through the array, element by element, and sets any value that is less than 0.2 to 0 and any value that is greater than (or equal to) 0.2 to 1.
Your code looks like this, with one line missing (replaced by ???). What command belongs in that line?
A = rand(4,7);
[M,N] = size(A);
for j = 1:M
????????????????????
if A(j,k) < 0.2
A(j,k) = 0;
else
A(j,k) = 1;
endif
endfor
endfor
I've created a gizmo with some sensors and buttons and whatnot, and it is all arduino based. I collect, process, and view the data through matlab on my desktop. The connection is through bluetooth and works well. Is there a way I can run that same code, or something every similar, on Matlab Mobile?
When I tried running my existing script on mobile it gave me an error about the arduino plug ins not on my phone.
Any ideas? Thanks!
I tried to use fft and ifft to calculate Electric Potential of a one-dimensional poisson equation, but went out wrong. However, the graphic itself is correct( look alike a cos(x) with corrrect frequency), but the amplitude is wrong and i can't find out why. Below is my code:
clear L=2pi; epsilon=1;% permittvity N=20480;% grid N=N+2; x=0:L/(N-1):L; xi=L/(N-1):L/(N-1):(L-L/(N-1)); ho=1sin(2pi/Lxi).cos(2pi/L*xi);rho=rho/epsilon;% equivilant charge distribution a=A(N); [u,rhok,N1]=poisson(rho,L,a); U=real(u); function [u,rhok,N1]=poisson(rho,L,a)
% The one-dimensional Poisson equation for periodic boundary conditions is calculated using the fast Fourier transform, where L is the total length and rho is the dielectric constant of the medium divided by epsilon
rhok=fft(rho);
N1=length(rho)+1;
delta=L/N1;%the size of the grid
uk(2:N1)=-(rhok*delta^2)./(-2+2*a(2:N1));% inside the area of solution
u(2:N1)=ifft(uk(2:N1));% inverse fft
u(1)=2*u(2)-u(3);
u(N1+1)=u(1);% boundary conditionn
end function a=A(N) for i=0:N-1 A(i+1)=cos(2pii/N); end end
I want to edit this parametre to control which variant to choose but its greyed out and cant be edited anyone have an idea what might be the problem?
is there still a discord for matlab?
I'm trying to create a car suspension, and no matter what i do, the position is always reported as -10.0 cm, and it is supposed to be -0.998. i have no idea what I'm doing wrong and I'm at my wits end, pls help, yes I've tried switching both springs and the dampener around to switch the c and r ports, but nothing works, I've gone over all the numbers a 100 times, please help.
I am trying to model a nonlinear capacitor in simulink whose capacitance is given by the equation C =8.646e-10* sqrt(1/abs(v)), where V is the voltage across the capacitor. How can I do it?
If you maintain MATLAB code and share it publicly for others to use, Vijay Iyer is the guy to follow!
Check out his inaugural blog post.