y=f(x) where x has different values. if x<0 y=sinx^2+ abs in matlab

You can define a function in MATLAB that takes in a vector of x values and computes the corresponding y values using the provided formula. Here's an example implementation:

main.m
function y = computeY(x)
    % Initialize y vector with 0s
    y = zeros(size(x));
    
    % Loop through each element of x
    for i = 1:length(x)
        % Check if x is negative
        if x(i) < 0
            % Compute y using sin and absolute value functions
            y(i) = sin(x(i)^2) + abs(x(i));
        else
            % Compute y using a different formula for positive x
            % (you can modify this as needed)
            y(i) = x(i)^2 + 1;
        end
    end
end
488 chars
18 lines

To use this function, you can call it with a vector of x values:

main.m
x = [-2 -1 0 1 2];
y = computeY(x);
disp(y);
45 chars
4 lines

This will output the corresponding y values for each x value:

main.m
   5.9093    1.8415    1.0000    2.0000    5.0000
50 chars
2 lines

gistlibby LogSnag