Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Conditional Coding

Kami_Munchkins

New Coder
I want it to set variable g depending on whether it is a sin, sawtooth or cos. I though i should set it dependent on the input on variable x, however i can't seem to get it to work... I am very new to coding, self taught and would appreciate any help
Code:
clear all;
close all;
clc;
A = 3;
phi = 90;
f1 = 0.5;
SampleRate = 40000;
t1=-2*pi:pi/SampleRate:2*pi;
x = input('name');
if  x == sin
     g = A*sin(2*pi*f1*t1+phi);
elseif x == cos;
        g = A*cos(2*pi*f1*t1+phi);
elseif x == sawtooth
    g = (-(2*A)/pi)*(atan(cot((pi)/(1/f1)*t)))
end
 
Some questions in random order :-

1) What language is this ? Certainly not C.
2) Are you getting any error messages at all ?
3) Why do you have a semicolon at the end of line 12 ? Do you know what the effect of that is ?
4) What is the meaning of this line t1=-2*pi:pi/SampleRate:2*pi; ?
5) Are you sure a statement like if x == sin works ? Should that not be if x == 'sin' ?
6) Is the value of variable pi defined anywhere ?
7) What are those first 3 statements supposed to do ?

EDIT - Now I see from your other post this is matlab code. Should not have been posted in the C forum.
 
Hi.

Is this matlab code?

Im not sure, what you are up with the code, i suggest you to comment your code much more better.

For some reason, i cant now use code tags, so lets do without.

Should this: g = (-(2*A)/pi)*(atan(cot((pi)/(1/f1)*t)))
be like this: g = (-(2*A)/pi)*(atan(cot((pi)/(1/f1)*t1)))
 
Last edited by a moderator:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom