Main Content

Results for


Thats the task:
Given a square cell array:
x = {'01', '56'; '234', '789'};
return a single character array:
y = '0123456789'
I wrote a code that passes Test 1 and 2 and one that passes Test 3 but I'm searching a condition so that the code for Test 3 runs when the cell array only contains letters and the one for Test 1 and 2 in every other case. Can somebody help me?
This is my code:
y = []
[a,b]=size(x)
%%TEST 3
delimiter=zeros(1,a)
delimiter(end)=1
delimiter=repmat(delimiter,1,b)
delimiter(end)=''
delimiter=string(delimiter)
y=[]
for i=1:a*b
y = string([y x(i)])
end
y=join(y,delimiter)
y=erase(y,'0')
y=regexprep(y,'1',' ')
%%TEST 1+2
for i=1:a*b
y = string([y x(i)])
y=join(y)
end
y=erase(y,' ' )
Here's a screenshot from 22 years ago. Thanks for building one of the best engineering and science communities together.
That's the question: Given four different positive numbers, a, b, c and d, provided in increasing order: a < b < c < d, find if any three of them comprise sides of a right-angled triangle. Return true if they do, otherwise return false .
I wrote this code but it doesn't pass test 7. I don't really understand why it isn't working. Can somebody help me?
function flag = isTherePythagoreanTriple(a, b, c, d)
a2=a^2
b2=b^2
c2=c^2
d2=d^2
format shortG
if a2+b2==c2
flag=true
else if a2+b2==d2
flag=true
else if a2+c2==d2
flag=true
else if c2+b2==d2
flag=true
else flag=false
end
end
end
end
end
That's the question:
The file cars.mat contains a table named cars with variables Model, MPG, Horsepower, Weight, and Acceleration for several classic cars.
Load the MAT-file. Given an integer N, calculate the output variable mpg.
Output mpg should contain the MPG of the top N lightest cars (by Weight) in a column vector.
I wrote this code and the resulting column vector has the right values but it doesn't pass the tests. What's wrong?
function mpg = sort_cars(N)
load cars.mat
sorted=sortrows(cars,4)
mpg = sorted(1:N,2)
end
cities
15%
beaches, islands, or cruises
22%
rivers, lakes, or mountains
33%
National Parks or historical sites
14%
wherever my family lives
12%
somewhere else
5%
13709 votes
I am trying to make a simulink model to use a MPC to reduce power consumption of HVAC system in an electric vehicle during cool down from ambient temperature to a set point temperature. Any help regarding this would be appreciated
Hi Everyone,
I have a high-order filter and I need to determinate its R,L,C values. In the simulation, the filter is unstable for some values. But I want to find values for stability of the filter. For second order system has some formulation between Q and phase margin so that it can be obtain the stability ciriteria to choose components.
My transfer function denominator is 5th order polynomial, and it is not possible to find roots in terms of R,L,C, Lm. I need to factor it like one 1th and two second order as shown in below.
D(s) = (1+s/wp) (1+s/wo1Q1 + (s/wo1)²) (1+s/wo2Q2 + (s/wo2)²)
Is there any methods to reduce order of transfer function?(Maybe, it can be possible for only in certain frequency range, can be renounced high-frequency )
Hello, an intern working at MathWorks is finishing up his program soon and he would like to interview some MATLAB users. He is looking for people who can give their perspective on the question:
"What makes MATLAB and Simulink special in comparison to other languages?"
Ultimately he plans to condense the answers into 15-second videos or sound bites.
If people are willing to participate but want more time to talk about their experience with MATLAB, he doesn't have time left for in-depth interviews but he can find someone else to take over the project.
Please send me an email via my profle if you are interested.
Yes, in my company that I own
35%
Yes, for someone else (or Univ.)
21%
Only for free, for charities
13%
Only in my charitable foundation
5%
No, I'd just play, travel, & relax
25%
15668 votes
Kindly help me correct this code to function properly. I am just learning MATLAB. i cannot get the output in abc frame. This is the code:
%----------- Define input and state parameters-----------------------------
clc
v_dc = 350; % DC input voltage in V
m = 0.841; % modulation index
C = 4000e-6; % DC buss capacitance in uf
L_1 = 2.5e-3; % Inverter side inductance in mH
L_2 = 2.5e-3; % Load side inductance in mH
L = 0; % load inductance
C_f = 10e-6; % filter capacitance in uf
R_f = 0.7; % damping resistance in ohms
R_L = 20; % load resistance in ohms
f_s = 10e3; % switching frequency
f = 60; % System frequency
R_s = 0.01; % Capacitance of the DC circuit
I_d = 8.594; % steady state current
w = 2*pi*f; % System angular Frequency
% Define initial steady state values
v_c = 349.4; i_d = 8.594; i_q = -0.213; v_df = 285; v_qf = -120; i_Ld = 8.594; i_Lq = 0.85;
%------------------S V P W M Generator-------------------------------------
% Define reference vector Uref
U_mag = m*v_dc/2; % Magnitude of Uref
% Define switching vectors
U1 = [v_dc/2;0]; % Vector Q1
U2 = [v_dc/4;sqrt(3)*v_dc/4]; % Vector Q2
U3 = [-v_dc/4;sqrt(3)*v_dc/4]; % Vector Q3
U4 = [-v_dc/2;0]; % Vector Q4
U5 = [-v_dc/4;-sqrt(3)*v_dc/4]; % Vector Q5
U6 = [v_dc/4;-sqrt(3)*v_dc/4]; % Vector Q6
% Define sector angles
theta1 = pi/6;
theta2 = pi/2;
theta3 = 5*pi/6;
theta4 = 7*pi/6;
theta5 = 3*pi/2;
theta6 = 11*pi/6;
% Define duty cycles for each switch using a for loop
for t=0:1/f_s:1/f % Time variable from 0 to one cycle of system frequency with steps of switching frequency
U_phase = w*t; % Phase of Uref (t is time variable)
U_alpha = U_mag*cos(U_phase); % Alpha component of Uref
U_beta = U_mag*sin(U_phase); % Beta component of Uref
if (0 <= U_phase) && (U_phase < theta1) % Sector 1
T1 = (sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T2 = (-sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T0 = 1 - T1 - T2;
d_a(round(t)+1) = T1 + T0/2;
d_b(round(t)+1) = T2 + T0/2;
d_c(round(t)+1) = T0/2;
elseif (theta1 <= U_phase) && (U_phase < theta2) % Sector 2
T3 = (sqrt(3)*U_beta - U_alpha)/(2*v_dc);
T2 = (sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T0 = 1 - T3 - T2;
d_a(round(t)+1) = T0/2;
d_b(round(t)+1) = T2 + T0/2;
d_c(round(t)+1) = T3 + T0/2;
elseif (theta2 <= U_phase) && (U_phase < theta3) % Sector 3
T3 = (sqrt(3)*U_beta - U_alpha)/(2*v_dc);
T4 = (-sqrt(3)*U_beta - U_alpha)/(2*v_dc);
T0 = 1 - T3 - T4;
d_a(round(t)+1) = T0/2;
d_b(round(t)+1) = T0/2;
d_c(round(t)+1) = T3 + T0/2;
elseif (theta3 <= U_phase) && (U_phase < theta4) % Sector 4
T5 = (-sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T4 = (-sqrt(3)*U_beta - U_alpha)/(2*v_dc);
T0 = 1 - T5 - T4;
d_a(round(t)+1) = T5 + T0/2;
d_b(round(t)+1) = T0/2;
d_c(round(t)+1) = T4 + T0/2;
elseif (theta4 <= U_phase) && (U_phase < theta5) % Sector 5
T5 = (-sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T6 = (sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T0 = 1 - T5 - T6;
d_a(round(t)+1) = T5 + T0/2;
d_b(round(t)+1) = T6 + T0/2;
d_c(round(t)+1) = T0/2;
elseif (theta5 <= U_phase) && (U_phase < theta6) % Sector 6
T1 = (sqrt(3)*U_beta + U_alpha)/(2*v_dc);
T6 = (sqrt(3)*U_beta - U_alpha)/(2*v_dc);
T0 = 1 - T1 - T6;
d_a(round(t)+1) = T1 + T0/2;
d_b(round(t)+1) = T0/2;
d_c(round(t)+1) = T6 + T0/2;
end
end
%-------------------------Define system matrices---------------------------
% Create Three-phase SVPWM VSI Inverter
% System matrix Nx-by-Nx matrix
A = [-1/(C*R_s),-sqrt(3)*m/(2*C),0,0,0,0,0;
sqrt(3)*m/(3*L_1),-R_f/(3*L_1),w,-1/(2*L_1),-sqrt(3)/(6*L_1),-R_f/(3*L_1),0;
0,-w,-R_f/(3*L_1),-sqrt(3)/(6*L_1),-1/(2*L_1),0,R_f/(3*L_1);
0,1/(2*C_f),-sqrt(3)/(6*C_f),0,w,-1/(2*C_f),sqrt(3)/(6*C_f);
0,sqrt(3)/(6*C_f),1/(2*C_f),-w,0,-sqrt(3)/(6*C_f),-1/(2*C_f);
0,R_f/(3*(L_2+L)),0,1/(2*(L_2+L)),sqrt(3)/(6*(L_2+L)),((-3*R_L-R_f)/(3*(L_2+L))),w;
0, 0, R_f/(3*(L_2+L)), -sqrt(3)/(6*(L_2+L)), 1/(2*(L_2+L)), -w, ((-3*R_L-R_f)/(3*(L_2+L)))];
% Define input matrix
B = [1/(C*R_s),-sqrt(3)*i_d/(2*C);d_a*v_dc,(sqrt(3)*v_c)/L_1;d_b*v_dc,0;d_c*v_dc,0;0,0;0,0;0,0]; % Nx-by-Nu input matrix
% Define output matrix
C = [0 1 0 0 0 0 0; % Ny-by-Nx matrix
0 0 1 0 0 0 0;
0 0 0 1 0 0 0;
0 0 0 0 1 0 0;
0 0 0 0 0 1 0;
0 0 0 0 0 0 1];
% Feedthrough matrix
D = zeros(6, 2); % Ny-by-Nu matrix
% create state-space model object
sys = ss(A,B,C,D);
% Define initial conditions and input
x0 = [v_c; i_d; i_q; v_df; v_qf; i_Ld; i_Lq]; % Initial state vector
t = 0:1e-6:0.5; % Time vector for simulation
u = repmat([v_dc;m],1,length(t)); % repeat u for each time step
% Simulate the system
[y, ~, x] = lsim(sys, u, t, x0);
% Extract the states
v_c_sim = x(:, 1);
i_d_sim = x(:, 2);
i_q_sim = x(:, 3);
v_df_sim = x(:, 4);
v_qf_sim = x(:, 5);
i_Ld_sim = x(:, 6);
i_Lq_sim = x(:, 7);
% Extract the outputs
v_abc_sim = y(:, 1:3);
i_abc_sim = y(:, 4:6);
v_dq_sim = y(:, 4:5);
i_dq_sim = y(:, 2:3);
% Plot the variables
figure;
subplot(4, 2, 1);
plot(t, v_c_sim);
xlabel('Time');
ylabel('v_c');
title('Capacitor Voltage');
subplot(4, 2, 2);
plot(t, i_d_sim);
xlabel('Time');
ylabel('i_d');
title('d-Axis Current');
subplot(4, 2, 3);
plot(t, i_q_sim);
xlabel('Time');
ylabel('i_q');
title('q-Axis Current');
subplot(4, 2, 4);
plot(t, v_df_sim);
xlabel('Time');
ylabel('v_df');
title('d-Component Filter Voltage');
subplot(4, 2, 5);
plot(t, v_qf_sim);
xlabel('Time');
ylabel('v_qf');
title('q-Component Filter Voltage');
subplot(4, 2, 6);
plot(t, i_Ld_sim);
xlabel('Time');
ylabel('i_Ld');
title('d-Axis Load Current');
subplot(4, 2, 7);
plot(t, i_Lq_sim);
xlabel('Time');
ylabel('i_Lq');
title('q-Axis Load Current');
% Perform coordinate transformation from dq frame to abc frame for currents
i_a_sim = cos(w*t)*i_d_sim - sin(w*t)*i_q_sim;
i_b_sim = cos(w*t - 2*pi/3)*i_d_sim - sin(w*t - 2*pi/3)*i_q_sim;
i_c_sim = cos(w*t + 2*pi/3)*i_d_sim - sin(w*t + 2*pi/3)*i_q_sim;
% Perform coordinate transformation from dq frame to abc frame for voltages
v_a_sim = cos(w*t)*v_df_sim - sin(w*t)*v_qf_sim;
v_b_sim = cos(w*t - 2*pi/3)*v_df_sim - sin(w*t - 2*pi/3)*v_qf_sim;
v_c_sim = cos(w*t + 2*pi/3)*v_df_sim - sin(w*t + 2*pi/3)*v_qf_sim;
Many thanks
1
33%
2
34%
3
18%
4
5%
5
3%
6+
6%
1643 votes
Image Analyst
Image Analyst
Last activity on 19 Aug 2023

half full.
12%
half empty.
4%
both.
12%
twice as big as it needs to be.
15%
1/2 full of beer, 1/2 full of air.
17%
What glass???
39%
6618 votes
I recently have found that I am no longer able to give my difficulty rating for questions on Cody after sucessfully completing a question. This is obviously not a big deal, I was just wondering if this was an issue on my end or if there was some change that I was not aware of.
The option to rate does not pop up after solving a problem, and the rating in general does not even show up anymore when answering questions (though it is visible from problem groups).
The MATLAB Answers community is an invaluable resource for all MATLAB users, providing selfless assistance and support. However, with the emergence of AI-based chatbots, like chatGPT, there may be concerns about the future relevance and utility of the MATLAB Answer community. What are your thoughts?
Hello, Recently I have started working on a thermal management project at my Institute. I am taking some hints from en example of EV thermal management provided by MATLAB. I am having an issue in deciding how the battery current and powertrain heat load is calculated as a function of vehicle speed. It would be helpful if someone could provide some links or references regarding this. (Also, attaching a screenshot of the example for better understanding)
figure out what my old code does
18%
write code comments for future me
11%
address a code analyzer warning
3%
reflect on the skills I've gained
8%
get food on my mouse and keyboard
38%
All of the above
22%
16434 votes
Getting Started with Simulink | Tips and Tricks to Get the Most Out of Simulink
Watch live as Tianyi Zhu and Ashwin Dhawad walk through how you can get started with Simulink by modeling, simulating, and tuning a PID controller for a DC Motor. This session isn’t just for beginners; they will show you the latest and greatest tips and tricks to help you get the most out of Simulink.
Date & Time: May 25, 2023 at 11:00 am EDT (see time in your time zone)
I am processing ocean and climate data (1982-2022). Here, I have table which having heatwaves events details. Now, I want to sort the data month wise and also seasonal wise (summer:October- February, and winter:March-September), and then i wanted to plot mean seasonal and trend.
Sample file is attached. length of original file may vary in size.
Thanks in addvance.
MATLAB Central has been great community-based MATLAB resources, but you can now access its content programmatically via the public API, and I created a MATLAB function to take advantage of that. You can learn more here https://api.mathworks.com/community
Example:
data = searchMATLABCentral("plotting",scope="matlab-answers",sort_order="created desc",created_after=datetime("2023-01-01"));
T = struct2table(data.items);
T(:,["created_date","title","is_answered"])
Output
Function
function results = searchMATLABCentral(query,options)
% SEARCGMATLABCENTRAL retrieves content of the MATLAB Central for a given
% query and returns the result as a struct.
% The function uses MathWorks RESTful API to search for content.
% The API is rate limited via IP throttling. No authentication is required.
% See API documentation for more details https://api.mathworks.com/community
%
% Input Arguments:
%
% query (string) - Required. The search query string.
% scope (string) - Optional. Specify the artifact. If not specified,
% the scope defaults to 'matlab-answers'.
% Other options include 'file-exchange','blogs','cody',
% 'community-highlights', and 'community-contests'.
% tags (string) - Optional. Specify a comma-separated list of tags.
% created_before (datetime) - Optional. Specify the last date in the results
% created_after (datetime) - Optional. Specify the first date in the results
% sort_order (string) - Optional. Speficy the order of the results.
% If not specified, it defaults to "relevance desc".
% Other options include 'created asc', 'created desc',
% 'updated asc','updated desc', 'relevance asc',
% and 'relevance desc'.
% page (integer) - Optional. Specify the page to retrieve.
% If the 'has_more' field in the result is positive,
% increment this argument to retrieve the next page.
% count (integer) - Optional. Specify the number of results as a value
% between 1 and 50; The default is 10.
%
% Output Arguments:
%
% results (struct) - Structure array containing the results of the search.
% validate input arguments
arguments
query string {mustBeNonzeroLengthText,mustBeTextScalar}
options.scope string {mustBeMember(options.scope,["matlab-answers", ...
"file-exchange","blogs","cody","community-highlights", ...
"community-contests"])} = "matlab-answers";
options.tags string {mustBeNonzeroLengthText,mustBeVector}
options.created_before (1,1) datetime
options.created_after (1,1) datetime
options.sort_order string {mustBeMember(options.sort_order,["created asc", ...
"created desc","updated asc","updated desc","relevance asc","relevance desc"])}
options.page double {mustBeInteger,mustBeGreaterThan(options.page,0)}
options.count double {mustBeInteger,mustBeInRange(options.count,1,50)}
end
% API URL and endpoint
url = "https://api.mathworks.com/community";
endpoint = "/v1/search";
% convert MATLAB datetime to the internet datetime format string
if isfield(options,"created_before")
options.created_before = string(options.created_before,"yyyy-MM-dd'T'HH:mm:ss'Z'");
end
if isfield(options,"created_after")
options.created_after = string(options.created_after,"yyyy-MM-dd'T'HH:mm:ss'Z'");
end
% convert optional inputs into a cell array of key-value pairs
keys = fieldnames(options);
vals = struct2cell(options);
params = [keys,vals].';
% call the API
try
results = webread(url+endpoint,"query",query,params{:});
catch ME
rethrow(ME)
end
end
Congratulations, @John D'Errico for winning the Editor's Pick badge awarded for MATLAB Answers, in recognition of your awesome solution in What to be chosen c so that all elements of matrix B will be a round number. B=A.c; where A is the matrix of non integers. The OP has not accepted his answer yet, but
As with other nominated answers, you provided detailed explanation with examples to show that what the OP is looking for may not be possible in some cases.
Unfortunately, you can only get this badge once - perhaps this raises a question about how this award should work. In any case, we nonetheles appreciate your ongoing effort to help other users in the community. Thnk you so much!
MATLAB Central Team