/r/matlab

Photograph via //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.

r/matlab discord channel


Sort By Topic

    Homework     Technical

    Code Share   News

    Tips             Misc


Places to learn Matlab
Matlab Resources

Try saturnapi to share and run MATLAB code in a web browser!

If you want flair simply Message the mods


/r/matlab

57,721 Subscribers

0

Help! Noob with code. Had to urgently do a PDE solving, none of the AI is helping. Error after error. What is wrong with the code.

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');

3 Comments
2024/05/04
17:40 UTC

2

Do you find MATLAB better than adobe photoshop for image processing?

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.

4 Comments
2024/05/04
15:27 UTC

1

Help in Simulink

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?

0 Comments
2024/05/04
10:17 UTC

1

Error in iterating over two loops

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!

4 Comments
2024/05/04
06:01 UTC

1

Matlab .txt File Help

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.

https://preview.redd.it/vebn7e692byc1.png?width=3420&format=png&auto=webp&s=261f9cbf68affd9026b48d5490aace3ffda4b6fd

https://preview.redd.it/7ohhcj592byc1.png?width=3419&format=png&auto=webp&s=406023798daed122facb0655662bfa5ed2260500

https://preview.redd.it/g9azio592byc1.png?width=3420&format=png&auto=webp&s=761bc2e58e25b271605e8601bd12693b951fef0e

https://preview.redd.it/34l5vp592byc1.png?width=3419&format=png&auto=webp&s=3fdb552b211d930391ec72d801969166c174b496

1 Comment
2024/05/04
00:35 UTC

1

ROS toolbox

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?

0 Comments
2024/05/03
22:08 UTC

0

Fmincon doesn't work properly

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?

3 Comments
2024/05/03
21:10 UTC

6

BALLANCING ROBOT NOOB

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.

4 Comments
2024/05/03
20:44 UTC

0

Can i Solve this in Matlab ? To linearize a nonlinear object using feedback and synthesize control ensuring a critically damped transient response with double poles at λ = -2.5.

7 Comments
2024/05/03
16:26 UTC

2

Want to study simulink

Best courses to study simulink :- certificate will be there or any YouTube channel

1 Comment
2024/05/03
16:07 UTC

1

VARX model predictions

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 !!!

0 Comments
2024/05/03
14:12 UTC

1

Matrix equation output

https://preview.redd.it/3863b276f7yc1.png?width=368&format=png&auto=webp&s=e0af1243998d142a7c7e4cb196f1a5f5b4077c10

This is output in Maxima. How can I make this in Matlab?
I tried disp but it can show one thing at the time

1 Comment
2024/05/03
12:22 UTC

1

Long vertical "evaluate at" bar in Live Script Equation

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?

https://preview.redd.it/ms475epy17yc1.png?width=446&format=png&auto=webp&s=55f3ff895d16292584b502a3b888461239e21ad7

0 Comments
2024/05/03
11:08 UTC

0

I need help with complex numbers in Matlab

Hello,
I'm solving an equation and the output is this:

4435179829497355625/18014398509481984 + 782041868234353125i/18014398509481984

And I need it to be written like this:

https://preview.redd.it/3omfa3m3f6yc1.png?width=167&format=png&auto=webp&s=e4ef51d4ec70dca3076642bcbfc1f0f1ccd2b6d0

If anyone knows, I would be veryyyy grateful!!!!!

3 Comments
2024/05/03
09:00 UTC

0

trying to understand what k1 and k2 are, with PID controller

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

https://preview.redd.it/bkw1pc6ix5yc1.jpg?width=700&format=pjpg&auto=webp&s=c55800915afaec4add5b3d595c93dea328640b09

7 Comments
2024/05/03
07:20 UTC

4

Im trying to implament a 2nd Order Differential Equation using the ode45 function in Matlab and I ran into some errors with Line 4 and 17. This is for a project at my university. I talked to a friend to help debug some of my errors, but he couldn't fix the following errors listed in the picture.

14 Comments
2024/05/03
04:51 UTC

1

Data Acquisition Best Data Type?

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.

4 Comments
2024/05/03
02:46 UTC

1

Translate Ball Bouncing Game to Matlab

2 Comments
2024/05/02
23:41 UTC

7

Fun tricks with MATLAB strings

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.

Fun tricks with MATLAB strings

4 Comments
2024/05/02
22:14 UTC

0

Help me please hurry! What is the solution? This is for octave and not Matlab btw, but I don't of any differences.

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
6 Comments
2024/05/02
22:04 UTC

2

I'm trying to do row reduction of a 6x6 Matrices, and I keep getting "*" instead of 0 when trying to format the Matrix into triangular form (row echelon form). My professor doesn't want the "*" symbol and wants 0s instead and I don't know how I can cancel out rows without getting the "*" symbol.

6 Comments
2024/05/02
21:09 UTC

2

Can I connect to arduino via matlab mobile?

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!

0 Comments
2024/05/02
16:19 UTC

1

Can't work out the solution of one dimensional poisson equation!

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

0 Comments
2024/05/02
16:07 UTC

2

Variant activation problem

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?

4 Comments
2024/05/02
15:12 UTC

0

MatLab homework I don't know how to start do you have any suggestion ?

8 Comments
2024/05/02
13:34 UTC

0

Discord

is there still a discord for matlab?

0 Comments
2024/05/02
02:12 UTC

3

Edx Course project, Need help ASAP! it just wont work!

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.

https://preview.redd.it/vdvwadcznwxc1.png?width=1426&format=png&auto=webp&s=7f8dc12a4c861b60176297bdf39c143a28983adf

https://preview.redd.it/5ru4pecznwxc1.png?width=1912&format=png&auto=webp&s=645e1a20d93caa147879dbbb47cef3f4ba896bb7

4 Comments
2024/05/02
00:13 UTC

3

How to model nonlinear capacitor in simulink?

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?

6 Comments
2024/05/01
22:02 UTC

8

🕵🏽‍♂️Slowly coming out of stealth mode... MATLAB Community Toolbox Program

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.

https://blogs.mathworks.com/matlab/2024/05/01/leading-by-example-how-lively-examples-help-matlab-community-toolboxes-grow-their-capabilities-communities/

2 Comments
2024/05/01
20:51 UTC

Back To Top