0
% create variable x with array of all necessary values
x=linspace(0.1,13,50);

for i=x
    % create equation to determine y
    y=(sqrt(2.*i)*4*i.^3)/(4.*i+7.^(i/10));
    %create equation to determine z
    z=log10(2.*i+5)+(4.*i+exp(i))/(2./3+4.*i.^2);
end

Using Matlab and im trying to use values from my x array to create two arrays, y and z, im pretty new to matlab and im struggling, thanks.

2
  • i got it figured out Commented Jan 15, 2020 at 1:47
  • Avoid using i as a variable name. It shadow the built-in i function that return the imaginary unit. Also in your example, you don't need to use the element-wise operators (.^, .*,./) if i is a scalar. The best option is to use the element-wise operators directly with x, for example y = sqrt(2*x)./x Commented Jan 15, 2020 at 10:08

2 Answers 2

1

The problem in you code is that you did not use for loop properly. You can run through the index of x, then assign x(i) to a new variable k in each iteration, i.e.,

x=linspace(0.1,13,50);

for k = 1:length(x)
    i = x(k);
    % create equation to determine y
    y(k) =(sqrt(2.*i)*4*i.^3)/(4.*i+7.^(i/10));
    %create equation to determine z
    z(k) =log10(2.*i+5)+(4.*i+exp(i))/(2./3+4.*i.^2);
end

Since MATLAB is able to vectorize the operations, so you are recommended to do it like below to speed up (for loop in MATLAB is expensive)

x = linspace(0.1,13,50);
y = (sqrt(2*x).*4.*x.^3)./(4*x+7^(x/10));
z = log10(2*x+5)+(4*x+exp(x))./(2/3 + 4*x.^2);

Remarks: you should be careful about the difference between .* and *, or ./ and /, where * and / are not element-wise operations.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that it is possible to use for i = x and will loop through objects. This feature only works with arrays (so the initialization with linspace() is OK). If you have a vector (size ?x1) it will use the entire row as the only iterating object. Though object-looping is quite nifty, it is unintuitive if you learned loop from low-level languages as C (or worse). For clarity, it is better to use the "normal" index looping as @ThomasIsCoding suggested =)
0

Method 1:

you can initialize y and z into empty arrays and just append the corresponding result at the end:

% create variable x with array of all necessary values
x=linspace(0.1,13,50);

y=[];
z=[];

for i=x
    % create equation to determine y
    y(end+1)=(sqrt(2.*i)*4*i.^3)/(4.*i+7.^(i/10));
    %create equation to determine z
    z(end+1)=log10(2.*i+5)+(4.*i+exp(i))/(2./3+4.*i.^2);
end

This approach can prove to be poor in terms of efficiency, since the size of y and z changes dynamically.

Method 2:

If you still want to use a for loop, it is better to preallocate memory for y and z and iterate the indices of x, something like:

% create variable x with array of all necessary values
x=linspace(0.1,13,50);

% Memory allocation
y = zeros(1, length(x));
z = zeros(1, length(x));

for i = 1 : length(x)
    % create equation to determine y
    y(i)=(sqrt(2.*x(i)*4*x(i).^3)/(4.*x(i)+7.^(x(i)/10));

    %create equation to determine z
    z(i)=log10(2.*x(i)+5)+(4.*x(i)+exp(x(i)))/(2./3+4.*x(i).^2);
end

Method 3 (preferred one):

It is generally more efficient to vectorize your implementations. In your case you could use something like:

x = linspace(0.1,13,50);
y = (sqrt(2.*x)*4*.*x.^3) ./ (4*x + 7.^(x./10));
z = log10(2*x+5) + (4*x + exp(x)) ./ (2/3 + 4*x.^2);

1 Comment

You should allocated memory beforehand (e.g. with NaN()) for variables y and z for efficiency. Matlab stores arrays contiguously in the RAM and; thus, relocates them every time it outgrows the curent position

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.