ANUPAM BISHT
ANUPAM BISHT

Reputation: 31

A matlab code for a simple function with a condition

I am given a function g1 = 1 - d/r1 and g2 = 1 - d/r2 Here d= 1, and r1 and r2 have been defined with a range 1 to 10 with spacing 0.1. I am trying to plot g1 vs g2, only those values which lie between 0 <= g1*g2 <= 1.

The code is

clc
clear all
close all

d = 1;
r1 = 0:0.1:10;
r2 = 0:0.1:10;
g1 = 1-(d./r1);
g2 = 1- (d./r2);

s1 = size(g1);
s2 = size(g2);
for i = 1:101
    e = g1(1,i);
  for j = 1:101
     if((e*g2(1,j)) <= 1 && (e*g2(1,j)>= 0))
        plot(g1(1,j),g2(1,j),'o')
        hold on
    end
end

end

Upvotes: 1

Views: 42

Answers (1)

user2900180
user2900180

Reputation:

If you need a Cartesian product of g1 x g2, you can use meshgrid function;

[G1, G2] = meshgrid(g1, g2);
product = G1 .* G2;

mask = product >= 0 & product <= 1;
figure, 
plot(G1(mask),G2(mask),'o');

Upvotes: 1

Related Questions