Plot sin wave with changing amplitude

Hi,
I plot Bx and By but I want to changing amplitude as the follwing
B0 = 0.45;
wav = 0.0223;
z1= 1.75
err= 10
z = linspace(0,z1,10000);
segb = B0/err;
seglam = wav/err;
Bx = (B0+segb).*sin((2 * pi * z) / (wav+seglam));
By = (B0+segb).*cos((2 * pi * z) / (wav+seglam));
plot(z,Bx,z,By)
title('Introduce errors')
it work for fixed err but it is suppose that Bx changing in z direction with error +-10 %
The output from this code as
The true output should be
How I can do that

5 Comments

Are there supposed to be random fluctuations in the amplitude of Bx and By?
What function defines the amplitude? You need to do something like
amplitude = whatever, some vector as long as Bx and By
% Create amplitude modulated signal
signal = amplitude .* Bx; % Make sure you use dot star and not just star.
In case it's still not clear, you're getting exactly what you've written. You've asserted that the error is a constant value.
err= 10 % this is a constant
segb = B0/err; % this is a constant too
seglam = wav/err; % so is this
Bx = (B0+segb).*sin((2 * pi * z) / (wav+seglam));
The output amplitude is B0+segb, which is the same as saying 1.1*B0. The amplitude never changes because 1.1 never changes.
As an aside, I have to question whether the error is supposed to simultaneously influence both amplitude and frequency, or whether that's supposed to be two separate error sources.
I have already succeeded to plot it with a fixed error. I have written I want to modify this code to make error changes from +-10 % as I write in the first post.
These are the equations I have to plot image like the attached

Sign in to comment.

 Accepted Answer

Try this:
fontSize = 16;
B0 = 0.45;
wav = 0.0223;
z1= 1.75
z1 = 1.7500
err= 10
err = 10
z = linspace(0,z1,10000);
segb = B0/err;
seglam = wav/err;
Bx = (B0+segb).*sin((2 * pi * z) / (wav+seglam));
By = (B0+segb).*cos((2 * pi * z) / (wav+seglam));
subplot(3, 1, 1);
plot(z,Bx,z,By)
title('Introduce errors')
xlabel('z', 'FontSize',fontSize);
ylabel('B', 'FontSize',fontSize);
legend('Bx', 'By');
% Create amplitude that changes
Amplitude = 0.9 + 0.2 * rand(1, length(z));
subplot(3, 1, 2);
plot(z, Amplitude, 'b-')
grid on;
title('Amplitude vs. z')
xlabel('z', 'FontSize',fontSize);
ylabel('Amplitude', 'FontSize',fontSize);
% Multiply the two together
ABx = Amplitude .* Bx;
ABy = Amplitude .* By;
subplot(3, 1, 3);
plot(z,ABx,z,ABy)
grid on;
title('Amplitude Modulated Signals')
xlabel('z', 'FontSize',fontSize);
ylabel('B', 'FontSize',fontSize);
legend('Bx', 'By');

7 Comments

@Ali Gamal's "Answer" moved here:
Thanks for your reply.
I think the function defines the amplitude are (B0+segb) and (wav+seglam), I modified segb and seglam acording to error equal 10 % but the out is normal sin wave, it supposes to change with z-direction as the attached seconde image
err = linspace(-10,10,15000);
segb = B0./err;
seglam = wav./err;
Bx = (B0+segb).*sin((2 * pi * z) ./ (wav+seglam));
@Ali Gamal, thanks, that's a perfect example of how using non-descriptive variable names can lead to confusion. Here, I've made it simpler by getting, and plotting, both the amplitude (B0+segb), and period (wav+seglam) vs. z.
fontSize = 16;
numElements = 10000;
z1 = 1.75;
z = linspace(0, z1, numElements);
B0 = 0.45;
originalSignal = sin(2 * pi * z);
% Plot the original signal.
subplot(4, 1, 1);
plot(z, originalSignal, 'b-', 'LineWidth', 2)
grid on;
title('Introduce errors', 'FontSize', fontSize); % Whatever that means...
xlabel('z', 'FontSize', fontSize);
ylabel('B', 'FontSize', fontSize);
% Create amplitude that changes with z
ramp = linspace(-10, 10, numElements);
segb = B0 ./ ramp;
amplitude = B0 + segb;
% Create period that changes with z
wav = 0.0223;
seglam = wav ./ ramp;
period = wav + seglam;
% Plot amplitude vs. z.
subplot(4, 1, 2);
plot(z, amplitude, 'b-')
grid on;
title('Amplitude vs. z', 'FontSize',fontSize)
xlabel('z', 'FontSize',fontSize);
ylabel('Amplitude', 'FontSize',fontSize);
% Plot period vs. z.
subplot(4, 1, 3);
plot(z, period, 'b-')
grid on;
title('Period vs. z', 'FontSize',fontSize)
xlabel('z', 'FontSize',fontSize);
ylabel('Period', 'FontSize',fontSize);
% Create a signal where BOTH the amplitude
% and period change with z.
Bx = amplitude .* sin(2 * pi * z ./ period);
% Plot the final signal
subplot(4, 1, 4);
plot(z, Bx, 'b-', 'LineWidth', 2);
grid on;
title('Amplitude and Period Modulated Signals', 'FontSize', fontSize)
xlabel('z', 'FontSize', fontSize);
ylabel('B', 'FontSize', fontSize);
I want to plot z and bx like the red curve in the attached image
You can use xlim to zoom in on the part you want
xlim([0, 0.115]);
Thanks for your help, but the output is not the same output in the attached image.
@Ali Gamal if I add back in the By vector, you get this:
% Demo to place points randomly, but with a minimum spacing from other points.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
fontSize = 16;
numElements = 10000;
z1 = 1.75;
z = linspace(0, z1, numElements);
B0 = 0.45;
originalSignal = sin(2 * pi * z);
% Plot the original signal.
subplot(4, 1, 1);
plot(z, originalSignal, 'b-', 'LineWidth', 2)
grid on;
title('Introduce errors', 'FontSize', fontSize); % Whatever that means...
xlabel('z', 'FontSize', fontSize);
ylabel('B', 'FontSize', fontSize);
% Create amplitude that changes with z
ramp = linspace(-10, 10, numElements);
segb = B0 ./ ramp;
amplitude = B0 + segb;
% Create period that changes with z
wav = 0.0223;
seglam = wav ./ ramp;
period = wav + seglam;
% Plot amplitude vs. z.
subplot(4, 1, 2);
plot(z, amplitude, 'b-')
grid on;
title('Amplitude vs. z', 'FontSize',fontSize)
xlabel('z', 'FontSize',fontSize);
ylabel('Amplitude', 'FontSize',fontSize);
% Plot period vs. z.
subplot(4, 1, 3);
plot(z, period, 'b-')
grid on;
title('Period vs. z', 'FontSize',fontSize)
xlabel('z', 'FontSize',fontSize);
ylabel('Period', 'FontSize',fontSize);
% Create a signal where BOTH the amplitude
% and period change with z.
Bx = amplitude .* sin(2 * pi * z ./ period);
By = amplitude .* cos(2 * pi * z ./ period);
% Plot the final signal
subplot(4, 1, 4);
plot(z, Bx, 'b-', 'LineWidth', 2);
grid on;
hold on;
plot(z, By, 'r-', 'LineWidth', 2);
title('Amplitude and Period Modulated Signals', 'FontSize', fontSize)
xlabel('z', 'FontSize', fontSize);
ylabel('B', 'FontSize', fontSize);
legend('Bx', 'By')
xlim([0, 0.115]);
However the period and phase that you specified in your formulas to not produce the waveforms that you showed in your screenshot. Look, "By" is a cosine (red curve) and is max at zero, and that's not what you showed. So we can only get what you want in the screenshot if we add in some phase shift, rather than having a zero phase shift like your formulas currently have. Do you know the phase shift? If not, why not? If the overall goal is just to match some arbitrary screenshot, then you can do trial and error with the formula until it looks like what you want, for example
% Create a signal where BOTH the amplitude
% and period change with z.
phaseShiftX = -.005;
phaseShiftY = -.0065;
Bx = amplitude .* sin(2 * pi * (z - phaseShiftX) ./ period);
By = amplitude .* cos(2 * pi * (z - phaseShiftY) ./ period);
In fact, this is a request from a friend, I think there is something not true with this equation and output, I will contact him.
Thanks very much

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!